Python - PyODBC - 带循环的多个查询

标签 python sql-server python-3.x pyodbc

我在通过 Python 中的 pyODBC 向 SQL Server 2012 发送多个查询时遇到问题。 我有一个带有查询的 DataFrame,我想用它来查询数据库。像这样的事情:

cnxn = pyodbc.connect('DRIVER={SQL Server};SERVER=mySERVER;DATABASE=myDB;UID=myUID;PWD=myPSWD')
cursor = cnxn.cursor()
cursor2 = cnxn.cursor()

for i in range(len(myDataFrame.Column_w_Queries)):
    query = '"' + myDataFrame.Column_w_Queries[i] + '"'
    cursor.execute(query)
    one = cursor.fetchone()
    print(one)
此示例中的

查询“select * from [DB].[schema].[table1]”(包括引号)。

问题是,当我运行 cursor.execute(query) 时,出现以下错误:

ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Could not find stored procedure 'select * from [DB].[schema].[table1]'. (2812) (SQLExecDirectW)")

我做错了什么?

最佳答案

您必须去掉周围的双引号。例如,打开 SQL Server Management Studio,然后尝试运行:

"select * from [DB].[schema].[table1]"

您将收到错误:

Msg 2812, Level 16, State 62, Line 1
Could not find stored procedure 'select * from [DB].[schema].[table1]'.

现在尝试:

select * from [DB].[schema].[table1]

...它应该可以工作。祝你好运!

关于Python - PyODBC - 带循环的多个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30572857/

相关文章:

python - Numpy 使用 putmask 和索引替换数组中的值

python - 如何使用 matplotlib 绘制散点饼图

java - 给定网格的一些节点,查找飞机在网格上的位置

sql-server - 在 2 个 SQL Server 上呈现数据库,就像它们都在 1 个 SQL Server 上一样

python - 在 azure 批处理节点上安装 Microsoft ODBC Driver 13 for SQL Server

java - 我无法使用 hibernate 和 sql-server 2008 在 java 中调用存储过程

sql-server - sql server 2008 express 的内存使用量有限?

python - 在Python中获取文件的文件夹名称

python - 向 PostgreSQL 插入元组的问题

python-3.x - 模块 'hmac' 没有属性 'new'