在python中插入SQLite主键

时间:2015-03-16 12:02:13

标签: python sqlite

我正在尝试从我的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);

1 个答案:

答案 0 :(得分:3)

您必须为所有列提供值,或者明确命名要插入的列。 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))