[英]How do I correct syntax error when attempting sqlite upsert?
I am encountering a SQLite UPSERT syntax error that I am unable to understand/correct.我遇到了一个我无法理解/纠正的 SQLite UPSERT 语法错误。 I am referencing the documentation at https://sqlite.org/lang_UPSERT.html我正在参考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?相反,返回sqlite3.OperationalError: near "ON": syntax error
我做错了什么?
The syntax was correct.语法是正确的。 My version of SQLite was not new enough to take advantage of the functionality.我的 SQLite 版本不够新,无法利用该功能。 Sadly, upgrading SQLite isn't as easy as PIP install --upgrade...可悲的是,升级 SQLite 并不像 PIP install --upgrade 那样容易...
Here are a few handy tips for others that may encounter this:以下是一些可能遇到此问题的人的一些方便的提示:
Finding your current SQLite version查找您当前的 SQLite 版本
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从 SQLite 下载站点获取更新的版本(在本例中为 3.24+): https ://www.sqlite.org/download.html
Unzip, then save the .dll in [Python install directory]/DLLs folder.解压缩,然后将 .dll 保存在 [Python 安装目录]/DLLs 文件夹中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.