簡體   English   中英

嘗試 sqlite upsert 時如何糾正語法錯誤?

[英]How do I correct syntax error when attempting sqlite upsert?

我遇到了一個我無法理解/糾正的 SQLite UPSERT 語法錯誤。 我正在參考https://sqlite.org/lang_UPSERT.html 上的文檔

示例代碼:

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

相反,返回sqlite3.OperationalError: near "ON": syntax error我做錯了什么?

語法是正確的。 我的 SQLite 版本不夠新,無法利用該功能。 可悲的是,升級 SQLite 並不像 PIP install --upgrade 那樣容易...

以下是一些可能遇到此問題的人的一些方便的提示:

查找您當前的 SQLite 版本

import sqlite3
sqlite3.sqlite_version

從 SQLite 下載站點獲取更新的版本(在本例中為 3.24+): https ://www.sqlite.org/download.html

解壓縮,然后將 .dll 保存在 [Python 安裝目錄]/DLLs 文件夾中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM