简体   繁体   English

尝试 sqlite upsert 时如何纠正语法错误?

[英]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.

相关问题 按照流行的 Flask 教程尝试从数据库中读取时,如何解决 Flask 错误“((sqlite3.OperationalError)没有这样的表:)”? - How do I solve a Flask error '((sqlite3.OperationalError) no such table:)' when attempting to read from a database following a popular Flask tutorial? insert + upsert在sqlite3控制台中有效,但是在python上它显示语法错误 - insert + upsert works in sqlite3 console, but on python it shows syntax error 如何使用 SQLAlchemy 核心在 SQLite3 数据库上正确执行 UPSERT? - How do I correctly perform an UPSERT on a SQLite3 DB using SQLAlchemy Core? 为什么在尝试测试正确的用户输入时出现语法错误? - Why do I get a syntax error when trying to test for correct user input? 尝试将数据插入 postgresql 时出现语法错误 - syntax error when attempting to insert data into postgresql SQLite 与 Python 的正确语法 - Correct Syntax for SQLite with Python 如何修复sqlite3.OperationalError / sql语法错误? - How can I fix this sqlite3.OperationalError / sql syntax error? 试图对 CSV 文件的第二列求和但收到错误。 如何在不使用熊猫的情况下进行纠正? - Attempting to sum the second column of a CSV file but receiving an error. How can I correct without the use of pandas? 如何使用 SqlAlchemy 进行更新插入? - How to do an upsert with SqlAlchemy? 尝试使用 Pandas Lambda 更改行值时出现语法错误 - Syntax error when attempting to change row values with Pandas Lambda
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM