Python:Pyodbc 执行带参数的存储过程
我在使用 Python 3.4 执行 SQL Server 存储过程时遇到问题.我正在导入 Pyodbc 来创建连接,并且有几行应该将数据发送到存储过程.
I'm having trouble executing a SQL Server stored procedure with Python 3.4. I'm importing Pyodbc to create the connection and have a few lines that are supposed to sent data to a stored procedure.
我的 Python:
sql = "exec <stored_procedure_name> @int1=?, @int2=?, @string1=?, @int3=?"
params = (irow[0], irow[15], irow[2], irow[6])
cursor.execute(sql, params)
存储过程:
@int1 int,
@int2 int,
@string1 varchar(10),
@int3 int
AS
BEGIN
--Do stuff
END
我在执行脚本时没有在命令提示符中收到错误消息.当我打印
I'm not getting an errors in Command Prompt when I execute the script. When I print
print(sql, params)
我得到了一个奇怪的结果.没有将任何内容插入到存储过程中的目标表中.
I get a weird looking result. Nothing is inserted into the table that is being targeted in the stored procedure.
exec <stored_procedure_name> @int1=?, @int2=?, @string1=?, @int3=? (0, 0, 'string', 0)
我是 Python 新手,因此不胜感激.我在 SO 上看到的一切都没有帮助.
I'm new to Python so any insight would be greatly appreciated. Nothing I've seen on SO has helped.
我发现问题是因为没有开启自动提交.默认情况下,Pyodbc 的自动提交设置为 False.
I found that the problem was caused by not having autocommit turned on. Pyodbc has autocommit set to False by default.
要启用自动提交,您可以像这样声明连接:
To enable autocommit, you can declare the connection like:
cnxn = pyodbc.connect(driver="<driver>", server="<server>", database="<database>", uid="<username>", pwd="<password>", autocommit=True)