【问题标题】:Python - PyODBC - multiple queries with loopPython - PyODBC - 带循环的多个查询
【发布时间】:2015-08-14 20:59:39
【问题描述】:

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

query 在此示例中为 "select * from [DB].[schema].[table1]"(包括引号)。

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

ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]找不到存储过程'select * from [DB].[schema].[table1]'。(第2812章

我做错了什么?

【问题讨论】:

  • 你试过不带引号的查询吗?

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


【解决方案1】:

你必须去掉周围的双引号。例如,打开 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]

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

【讨论】:

  • 非常感谢。我发誓我在玩引号,但你的回答对我有很大帮助;-)
猜你喜欢
  • 2022-08-18
  • 2023-03-23
  • 1970-01-01
  • 2020-06-05
  • 1970-01-01
  • 2016-10-16
  • 2016-06-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多