[英]Incorrect syntax near 's' in pyodbc
Sometimes I get this error randomly when I try to insert data in to the database. 有时,当我尝试将数据插入数据库时会随机出现此错误。 I fetch the data with using request.get and parsing the JSON data. 我使用request.get提取数据并解析JSON数据。
This is the error that I get: 这是我得到的错误:
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)") pyodbc.ProgrammingError :(“ 42000”,“ [42000] [Microsoft] [用于SQL Server的ODBC驱动程序17] [SQL Server]“ s”附近的语法不正确。(102)(SQLExecDirectW); [42000] [Microsoft] [ODBC [SQL Server]驱动程序17 [SQL Server]字符串')'后的引号引起来。(105)“)
This is my code: 这是我的代码:
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']))
You must not use string interpolation for SQL queries. 不得对SQL查询使用字符串插值。 The db-api will do correct parameter substitution for you - replace that %
with a comma. db-api将为您执行正确的参数替换-用逗号替换该%
。
cursor.execute('SELECT.... ', (product['id'],product['created_at'...))
# ^
cursor.execute('SELECT.... product = ?', (value_for_product))
适用于python 3. ^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.