python - 带参数的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')
解决方案
尝试交换单引号和双引号。SQL 语句本身喜欢单引号,因此要实现使外引号双引号。Python 并不介意。
str1 = "EXEC dbo.spRunFile @logID={logid}, @fileName='{filename}'".format(filename='D:\\website\\1228file.txt',logid=1)
推荐阅读
- flask - ubuntu 16.04 上的 Python flask socketio(传输=池错误)
- outlook-addin - Outlook 加载项中的 RoamingSettings 似乎无法正常工作
- node.js - 如何在 Node JS 中设置 AWS s3 存储桶版本控制?
- c - 使用文件作为已编译 c 程序的输入
- javascript - 无法调用 javascript 和 css 外部文件
- node.js - 部署在 Azure 上的 node.js。没有安装依赖
- vue.js - 来自 Vue 路由器的 ViewBag.Title
- internet-explorer - Guided Tours Fail-on-Startup with Internet Explorer 11
- java - How to make swagger to handle wrappers with generics like Flux
? - javascript - *NgFor 切换显示/隐藏单个元素