在 Python 脚本中执行 SQL 存储过程

我正在尝试在 Python 脚本中执行 SQL 存储过程,但遇到语法问题。我尝试了我找到的各种示例的组合,但似乎没有一个起作用。这是我尝试过的:


county = 100200

confirmed = 123456

death = 12

labTestCount = 0    

#Example1

conn.execute('{CALL [spInsertCOVIDData](@County,@Confirmed,@Death,@LabTestCount)}', ('county', 'confirmed', 'death', 'labTestCount'))


#Example2

query = "EXEC [spInsertCOVIDData] (@County,@Confirmed,@Death,@LabTestCount)", (county, confirmed, death, labTestCount)

conn.execute(query)


#Example3

query = "EXEC [spInsertCOVIDData] @County=?, @Confirmed=?, @Death=?, @LabTestCount=?",(county, confirmed, death, labTestCount)

conn.execute(query)


#Example4

query = "EXEC [spInsertCOVIDData] @County='county', @Confirmed='confirmed', @Death='death', @LabTestCount='labTestCount'"

conn.execute(query)

我从以下位置得到了各种错误的混合:


('The SQL contains 0 parameter markers, but 4 parameters were supplied', 'HY000')

或者


The first argument to execute must be a string or unicode query.


慕无忌1623718
浏览 80回答 1
1回答

慕哥6287543

您可以尝试在语句中使用参数?作为占位符并传递每个参数的实际值:county = 100200confirmed = 123456death = 12labTestCount = 0query = "EXEC [spInsertCOVIDData] @County=?, @Confirmed=?, @Death=?, @LabTestCount=?"conn.execute(query, (county, confirmed, death, labTestCount))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python