简体   繁体   English

执行查询时出现 python pymssql 错误

[英]python pymssql error with query execution

This is the pymssql query I am working with这是我正在使用的 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'")

I'm using these values as an example, they are not the real values I'm trying to upload.我以这些值为例,它们不是我要上传的真实值。 However the errors are the same:但是错误是相同的:

109, b'There are more columns in the INSERT statement than values specified in the VALUES clause. 109, b'INSERT 语句中的列多于 VALUES 子句中指定的值。 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') VALUES 子句中的值数必须与 INSERT 语句中指定的列数匹配。DB-Lib 错误消息 20018,严重性 15:\n常规 SQL 服务器错误:检查来自 SQL 服务器的消息\n')

I'm not sure why these error is occurring as there's clearly 3 columns and 3 values being inserted.我不确定为什么会发生这些错误,因为显然插入了 3 列和 3 个值。

Any help on this would be appreciated对此的任何帮助将不胜感激

Your format placeholders might need commas between them:您的格式占位符可能需要在它们之间使用逗号:

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'")

Looking at the docs , it appears that pymssql might prefer you use C-style string-formatting symbols;查看文档,似乎pymssql可能更喜欢您使用 C 风格的字符串格式化符号; maybe something like this:也许是这样的:

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)

Here is another SO question with an example.是另一个带有示例的 SO 问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM