通过python运行简单查询:没有结果

2024-09-30 16:40:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试学习如何使用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显示结果方面,我肯定遗漏了一些东西。你知道吗


Tags: 模块数据文档importidrequest错误sys