【问题标题】:Can't query data using flask framework pymysql无法使用flask框架pymysql查询数据
【发布时间】:2020-02-25 16:26:29
【问题描述】:

pythonflask框架下,无法使用pymysql查询数据:

def sql_query (commit_ip):
    with db.cursor() as cursor:
        sql = "select client_ip, result, date_time from ipset where client_ip = 'commit_ip'"
        cursor.execute (sql)
        dict_ipset = cursor.fetchall ()
        return dict_ipset

commit_ip 变量只有替换成特定的 IP 后才能查询。 date_time 也是如此。 client_ip 的数据类型是 char (15)。这个sql应该怎么写?

【问题讨论】:

    标签: python-3.x pymysql


    【解决方案1】:

    您应该将变量值作为元组作为第二个参数传递给执行。来自PyMySql's doc的示例:

        sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
        cursor.execute(sql, ('webmaster@python.org',))
    

    所以对你来说是:

        sql = "select client_ip, result, date_time from ipset where client_ip = %s"
        cursor.execute(sql, (commit_ip,))
    

    【讨论】:

    • 谢谢你,伙计!我看到了 PyMySql 的文档,但是你知道有一个'',所以我弄错了。
    猜你喜欢
    • 2023-01-19
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 2013-04-25
    • 1970-01-01
    • 2017-07-27
    相关资源
    最近更新 更多