1

I am looking for some help with my Py code. It uploads to an Azure SQL using PYODBC MS 18.

When I insert I am using placeholders:

insert_sql ='''INSERT INTO table_name (ID, time, views, likes, shares, comments, URL) 
              VALUES (?, ?, ?, ?, ?, ?, ?)
#Issue line#      ON DUPLICATE KEY UPDATE ID=current_data[0], time=current_data[1], views=current_data[2], likes=current_data[3], shares=current_data[4], comments=current_data[5], URL=current_data[6]
                      '''
insert_data=[(current_data[0], current_data[1], current_data[2], current_data[3], current_data[4], current_data[5], current_data[6])]
crsr.executemany(insert_sql, insert_data)

Objective - Insert into table, If the ID is already in the table, replace the row with the same data.

Only because I just want it to update the same keys when needed.

Error:

pyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Incorrect syntax near the keyword 'ON'. (156) (SQLExecute); [42000] [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Statement(s) could not be prepared. (8180)")

Dale K
  • 25,246
  • 15
  • 42
  • 71
  • TSQL uses MERGE for this. https://learn.microsoft.com/en-us/sql/t-sql/statements/merge-transact-sql?view=sql-server-ver15#:~:text=MERGE%20%28Transact-SQL%29%201%20Index%20best%20practices.%20By%20using,INSERT%2C%20UPDATE%2C%20and%20DELETE%20statements.%203%20Examples.%20 – David Browne - Microsoft Feb 24 '22 at 22:29
  • Would you be able to assist in how this could be implemented? – Ellis Moore Feb 24 '22 at 23:02

0 Answers0