简体   繁体   中英

Inserting SQLite primary key in python

I am trying to do a SQLite insert from my python script.

I am getting an error:

table history has 4 columns but 3 values were supplied

The reason there is 3 not 4, is that the first one is my ID. I assumed that as i declared it as a primary key it would auto-increment the value. How do I go about inserting the 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);

You either have to provide values for all columns, or name the columns you are inserting into explicitly. SQLite will not go and guess what columns you provided values for, you need to be explicit about them.

You can use a NULL to have SQLite insert an auto-generated id for you:

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

or you can name the 3 columns you are inserting into explicitly:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM