python 执行查询时出现 pymssql 错误

python pymssql error with query execution

这是我正在使用的 pymssql 查询

query = 'INSERT INTO [dbo].[helios_devops_data_curr] ("iipm.l3_it_org", "iipm.it_custodian","iipm.it_executive") VALUES ({}{}{})'.format("'Innovation and Technology'", "'bob tom'", "'bob tom'")

我以这些值为例,它们并不是我要上传的真实值。但是错误是一样的:

109, b'There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.DB-Lib error message 20018, severity 15:\nGeneral SQL Server error: Check messages from the SQL Server\n')

我不确定为什么会出现这些错误,因为显然插入了 3 列和 3 个值。

如有任何帮助,我们将不胜感激

您的格式占位符之间可能需要逗号:

query = 'INSERT INTO [dbo].[helios_devops_data_curr] ("iipm.l3_it_org", "iipm.it_custodian","iipm.it_executive") VALUES ({}, {}, {})'.format("'Innovation and Technology'", "'bob tom'", "'bob tom'")

查看 the docs,似乎 pymssql 可能更喜欢您使用 C 风格的字符串格式符号;也许是这样的:

query = "INSERT INTO dbo.helios_devops_data_curr(iipm.l3_it_org, iipm.it_custodian, iipm.it_executive) VALUES (%s, %s, %s)"
params = ("Innovation and Technology", "bob tom", "bob tom")
cursor.execute(query, params)

是另一个带有示例的 SO 问题。