当我执行SQLite查询时,模糊的“RuntimeError:接近”exists“:语法错误”消息

Vague "RuntimeError: near "exists": syntax error" message when I execute a SQLite query

提问人:kimforestleaf 提问时间:11/4/2023 最后编辑:kimforestleaf 更新时间:11/4/2023 访问量:41

问:

[已解决:见第一条评论]

我的(SQLite)数据库中有这个表:

CREATE TABLE IF NOT EXISTS 'item_existences'(
  'id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
  'address_id' TEXT NOT NULL,
  'item_id' INTEGER NOT NULL,
  'exists' BOOLEAN NOT NULL
);

在phpLiteAdmin中看起来像这样:phpLiteAdmin中表的屏幕截图

我正在尝试使用在这些 python 代码行中运行的查询填充该表(借助 cs50 的 SQL 模块):

from cs50 import SQL

db = SQL("sqlite:///database.db")

for address_id in range(1,20):
    for item_id in range(1,40):
        db.execute("INSERT INTO item_existences (address_id, item_id, exists) VALUES (?, ?, ?)", address_id, item_id, "TRUE")
  • 但是我在控制台中收到以下“语法错误”消息,这并没有告诉我问题所在:
Traceback (most recent call last):
  File "/workspaces/96890493/homepage/db-manipulation.py", line 16, in <module>
    db.execute("INSERT INTO item_existences (address_id, item_id, exists) VALUES (?, ?, ?)", address_id, item_id, "TRUE")
  File "/usr/local/lib/python3.12/site-packages/cs50/sql.py", line 29, in decorator
    return f(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/cs50/sql.py", line 413, in execute
    raise e
RuntimeError: near "exists": syntax error

RuntimeError:near “exists”:语法错误

我想知道我必须在“存在”列中插入的布尔值的编写方式是否存在问题,这似乎是导致它的原因,我尝试使用数字“1”(作为整数,不带引号)代替,但它也没有做任何事情。我一遍又一遍地重读查询,但我似乎无法指出问题所在......

有没有人知道我在哪里犯了错误?

提前感谢您的帮助~ 🌸

SQLite 运行时错误 语法错误

评论

0赞 Shawn 11/4/2023
exists 是保留关键字。使用其他内容作为列的名称,或者通过在查询中将其放在双引号中来对其进行转义。... (address_id, item_id, "exists") ...
0赞 kimforestleaf 11/4/2023
哇,解决了,谢谢!☺️ (它适用于单引号而不是双引号) 永远不可能发现🥲这一点
0赞 Shawn 11/4/2023
SQL 对字符串使用单引号,并在需要时使用双引号对表名和列名等标识符进行转义。

答: 暂无答案