I am encountering a SQLite UPSERT syntax error that I am unable to understand/correct. I am referencing the documentation at https://sqlite.org/lang_UPSERT.html
Example code:
import sqlite3
cnxn = sqlite3.connect(":memory:")
crsr = cnxn.cursor()
# Create a simple table with a Primary Key
crsr.execute("CREATE TABLE t (Id INTEGER PRIMARY KEY, Name TEXT)")
# Add a row
crsr.execute("INSERT INTO t(Id,Name) VALUES(1,'Fred')")
# I would expect the following to update Name for Id=1
crsr.execute("INSERT INTO t(Id,Name) VALUES(1,'Sam') ON CONFLICT(Id) DO UPDATE SET Name=excluded.Name")
Instead, returns
sqlite3.OperationalError: near "ON": syntax error
What am I doing wrong?
The syntax was correct. My version of SQLite was not new enough to take advantage of the functionality. Sadly, upgrading SQLite isn't as easy as PIP install --upgrade...
Here are a few handy tips for others that may encounter this:
Finding your current SQLite version
import sqlite3
sqlite3.sqlite_version
Grab the newer version (3.24+ in this case) from the SQLite download site: https://www.sqlite.org/download.html
Unzip, then save the .dll in [Python install directory]/DLLs folder.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With