我正在尝试学习如何使用python和pyodbc模块获取microsoftsql查询结果,并且遇到了一个问题,即使用microsoftsqlmanagementstudio中使用的相同查询返回相同的结果。你知道吗
我已经查看了pyodbc文档并正确设置了连接。。。至少我在执行时没有收到任何连接错误。唯一的问题似乎是返回表数据
import pyodbc
import sys
import csv
cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=<server>;DATABASE=<db>;UID=<uid>;PWD=<PWD>')
cursor = cnxn.cursor()
cursor.execute("""
SELECT request_id
From audit_request request
where request.reception_datetime between '2019-08-18' and '2019-08-19' """)
rows = cursor.fetchall()
for row in cursor:
print(row.request_id)
当我运行上面的代码时,我在python终端窗口中看到:
Process returned 0 (0x0) execution time : 0.331 s
Press any key to continue . . .
我在sqlmanagementstudio中尝试了同样的查询,它返回了我想要的结果。在使用python显示结果方面,我肯定遗漏了一些东西。你知道吗
实际上,您并没有设置要使用的
cursor
。在执行之前,您应该执行以下操作:cursor = cnxn.cursor()
了解更多信息:https://github.com/mkleehammer/pyodbc/wiki/Connection#cursor
相关问题 更多 >
编程相关推荐