pyodbc 中 's' 附近的语法不正确

Incorrect syntax near 's' in pyodbc

有时,当我尝试将数据插入数据库时​​,会随机出现此错误。我使用 request.get 获取数据并解析 JSON 数据。

这是我得到的错误:

pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near 's'. (102) (SQLExecDirectW); [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Unclosed quotation mark after the character string ')'. (105)")

这是我的代码:

for product in parsed['products']:
                cursor.execute("INSERT INTO dbo.producten (productid, datum_toegevoegd, naam, prijs_excl, prijs_incl, gewicht) VALUES ('%s','%s','%s','%s','%s','%s')" %(product['id'],product['created_at'], product['nl']['title'],product['price_excl'],product['price_incl'],product['weight']))

您不得对 SQL 查询使用字符串插值。 db-api 将为您进行正确的参数替换 - 将 % 替换为逗号。

cursor.execute('SELECT.... ', (product['id'],product['created_at'...))
#                           ^
cursor.execute('SELECT.... product = ?', (value_for_product)) 

适用于 python 3.^