首页 > 解决方案 > 带参数的python SQL SP不起作用

问题描述

我的代码正在运行,但 SP 实际上并未执行。str1当我直接在 SQL Server 中运行打印时,它可以工作。我在玩 pyodbc:它甚至不在 SP 中。

代码:

str1 = 'EXEC dbo.spRunFile @logID={logid}, @fileName="{filename}"'.format(filename="D:\\website\\1228file.txt",logid=1)
print(str1)
print('going to excute cursor')
cursor.execute(str1)
print ('Cursor executed')

有什么建议么?

提前致谢!

我也尝试了这些变种

str1 = "EXEC dbo.spRunFile @logID={logid}, @fileName='{filename}'".format(filename="D:\website\1228file.txt",logid=1)

str1 = 'EXEC dbo.spRunFile @logID={logid}, @fileName="{filename}"'.format(filename="D:\website\1228file.txt",logid=1)

str1 = "EXEC dbo.spRunFile @logID={logid}, @fileName='{filename}'".format(filename='D:\website\1228file.txt',logid=1)

str1 = "EXEC dbo.spRunFile @logID={logid}, @fileName='{filename}'".format(logid=1,filename='D:\website\1228file.txt.txt')

标签: pythonsql-serverstored-procedures

解决方案


尝试交换单引号和双引号。SQL 语句本身喜欢单引号,因此要实现使外引号双引号。Python 并不介意。

str1 = "EXEC dbo.spRunFile @logID={logid}, @fileName='{filename}'".format(filename='D:\\website\\1228file.txt',logid=1)

推荐阅读