首页 > 解决方案 > 将查询结果保存在变量中

问题描述

import pymysql
connection = pymysql.connect(host='......', user='....', password='.....', database='......')
cursor=connection.cursor()
q1="select count(*) from lk_employee_data where date(entry_time)=('2020-11-18')"
cursor.execute(q1)

q1 查询给出结果 1。如果我在数据库中运行相同的查询,结果是 190。所以我想知道为什么它不能正常工作。我还想将 q1 查询的结果保存在变量中。

标签: pythonpymysql

解决方案


cursor.execute() 将返回执行的查询影响的行数。要检索数据,您必须直接访问游标结果,如下所示:

cursor = connection.cursor()
q1="select count(*) from lk_employee_data where date(entry_time)=('2020-11-18')"
cursor.execute(q1)
results = cursor.fetchall()

推荐阅读