简体   繁体   中英

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.

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

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. The db-api will do correct parameter substitution for you - replace that % with a comma.

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

适用于python 3. ^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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