我从 helpful post 中学到了在 StackOverflow 上关于如何在 python (pyodbc) 中调用 SQL Server 上的存储过程。将我的代码修改为以下内容后,我能够从我创建的 db_engine
连接并运行 execute()
。
import pyodbc
import sqlalchemy as sal
from sqlalchemy import create_engine
import pandas as pd
import urllib
params = urllib.parse.quote_plus(
'DRIVER={ODBC Driver 17 for SQL Server};'
f'SERVER=myserver.com;'
f'DATABASE=mydb;'
f'UID=foo;'
f'PWD=bar')
cobnnection_string = f'mssql+pyodbc:///?odbc_connect={params}'
db_engine = create_engine(connection_string)
db_engine.execute("EXEC [dbo].[appDoThis] 'MYDB';")
<sqlalchemy.engine.result.ResultProxy at 0x1121f55e0>
db_engine.execute("EXEC [dbo].[appDoThat];")
<sqlalchemy.engine.result.ResultProxy at 0x1121f5610>
然而,即使在 Python 中运行上述代码后没有返回任何错误,当我检查数据库时,我确认没有执行任何操作(更能说明问题的是,上述命令需要一两秒才能完成,而在数据库管理工具上成功运行这些存储过程大约需要 5 分钟)。
为了正确调试,我应该如何理解上述设置中哪些部分无法正常工作?我通过我的数据库管理工具运行完全相同的代码,没有任何问题——存储过程按预期执行。什么可以通过 Python 阻止这种情况发生?执行的SQL是否需要commit?有没有办法使用返回的 ResultProxy
进行调试?如有任何建议,我们将不胜感激。
最佳答案
直接在 Engine
对象上调用 .execute()
是一种过时的使用模式,从 SQLAlchemy 1.4 版开始将发出弃用警告。如今,首选方法是使用使用 engine.begin()
的上下文管理器(with
block ):
import sqlalchemy as sa
# …
with engine.begin() as conn: # transaction starts here
conn.execute(sa.text("EXEC [dbo].[appDoThis] 'MYDB';"))
# On exiting the `with` block the transaction will automatically be committed
# if no errors have occurred. If an error has occurred the transaction will
# automatically be rolled back.
注意事项:
- 当传递 SQL 命令字符串时,它应该被包装在 SQLAlchemy
text()
对象中。 - 在绝大多数情况下,SQL Server 存储过程(和匿名代码块)应该以
SET NOCOUNT ON;
开头。如果不这样做,可能会导致合法结果或错误“滞后于”可能由 DML 语句(如INSERT
、UPDATE
或)发出的任何行计数删除
。
关于python - 使用 Python 客户端在 SQL Server 中执行存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66557811/