python - 将查询结果保存在变量中
问题描述
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 查询的结果保存在变量中。
解决方案
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()
推荐阅读
- java - 索引返回错误值,即 1 而不是 0
- java - Dropwizard 拦截器响应状态
- javascript - 在javascript中的switch case中传递url参数
- python - 克隆和修改模块的github repo后如何导入python模块
- python - 如何使用命令来打破这个循环
- html - CSS SVG 过滤器:url(#element) 在 Firefox 中不起作用并且在 safari 中有奇怪的行为
- c# - 应用程序内的客户端-服务器安全通信
- python - 无法在 nano jetson 上安装 h5py
- php - 使用 PHP Simple HTML DOM 提取内容
- java - 在另一个类上调用 get 方法时获取空数据