python - 带有 Python "Table has X columns but Y were supplied"的 SQLite

标签 python sql sqlite

我有一个执行一些简单 SQL 的 python 脚本。

c.execute("CREATE TABLE IF NOT EXISTS simpletable (id integer PRIMARY KEY, post_body text, post_id text, comment_id text, url text);")
command = "INSERT OR IGNORE INTO simpletable VALUES ('%s', '%s', '%s', '%s')" % (comments[-1].post_body, comments[-1].post_id, comments[-1].comment_id, 
comments[-1].url)
c.execute(command)
c.commit()

但是当我执行它时,我收到一个错误

sqlite3.OperationalError: table simpletable has 5 columns but 4 values were supplied



为什么不自动填id key ?

最佳答案

如果不指定目标列 VALUES预计会为所有列提供值,而您没有这样做。

INSERT
 OR IGNORE INTO simpletable
                (text,
                 post_id,
                 comment_id,
                 text)
                VALUES ('%s',
                        '%s',
                        '%s',
                        '%s');

在任何情况下都建议指定目标列。如果出于任何原因,表中列的顺序发生变化,则查询不会中断。

关于python - 带有 Python "Table has X columns but Y were supplied"的 SQLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51643509/

相关文章:

按日期范围搜索的 SQL 查询

mysql - 查询不适用于单引号撇号

sqlite - 如何使用 SQLite/ Entity Framework 在 app.config 中创建数据库的相对路径

python - tox如何在pip install中提供可信主机?

python - pandas 填充日期+多列

python - Matplotlib/ basemap : Plot a globe in the center of a plot

python - Python 中的网页抓取

sql - 当没有 INSERT 语句时,为什么我得到 "String or Binary Data would be Truncated Error"?

java.lang.NullPointerException 查询错误

android - SQLite 中存储的图像未显示在 Gridview 中