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