gpt4 book ai didi

python - 在 python 中插入 SQLite 主键

转载 作者:行者123 更新时间:2023-12-01 22:32:16 25 4
gpt4 key购买 nike

我正在尝试从我的 python 脚本执行 SQLite 插入。

我收到一个错误:

table history has 4 columns but 3 values were supplied

之所以有 3 个而不是 4 个,是因为第一个是我的 ID。我假设当我将它声明为主键时,它会自动增加值。我该如何插入 ID

.py:

c.execute("INSERT INTO history VALUES (?,?,?)", (q[0],t,in))

.db

CREATE TABLE history (id integer primary key, employeeID integer, time datetime, inout varchar);

最佳答案

您要么必须为所有 列提供值,要么明确命名要插入的列。 SQLite 不会猜测您为哪些列提供了值,您需要明确说明它们。

您可以使用 NULL 让 SQLite 为您插入一个自动生成的 ID:

c.execute("INSERT INTO history VALUES (NULL,?,?,?)", (q[0],t,in))

或者您可以明确命名要插入的 3 列:

c.execute("INSERT INTO history (employeeID, time, inout) VALUES (?,?,?)", (q[0],t,in))

关于python - 在 python 中插入 SQLite 主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29076312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com