我在通过 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/