Reputation: 318
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']))
Upvotes: 0
Views: 7782
Reputation: 103
cursor.execute('SELECT.... product = ?', (value_for_product))
works for python 3.^
Upvotes: 1
Reputation: 599866
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'...))
# ^
Upvotes: 3