Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Can';使用flask框架pymysql查询数据_Python 3.x_Pymysql - Fatal编程技术网

Python 3.x Can';使用flask框架pymysql查询数据

Python 3.x Can';使用flask框架pymysql查询数据,python-3.x,pymysql,Python 3.x,Pymysql,在python flask框架下,不能使用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.fetchal

在python flask框架下,不能使用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

只有在用特定ip替换变量commit_ip后,才能查询该变量。日期和时间也是如此。客户端ip的数据类型是char(15)。如何编写此sql?

您应该将变量值作为元组传递,作为要执行的第二个参数。示例来自:

因此,对你来说,这将是:

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

谢谢你,伙计!我看到了PyMySql的文档,但正如你所知,这里有一个“”,所以我犯了一个错误
    sql = "select client_ip, result, date_time from ipset where client_ip = %s"
    cursor.execute(sql, (commit_ip,))