无法使用flask框架pymysql查询数据
Can't query data using flask framework pymysql
pythonflask框架下无法使用pymy查询数据sql:
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应该怎么写?
您应该将变量值作为元组作为第二个参数传递给执行。来自 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,))
pythonflask框架下无法使用pymy查询数据sql:
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应该怎么写?
您应该将变量值作为元组作为第二个参数传递给执行。来自 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,))