I have an SQLite table that uses an autogenerated id as primary key. I want to do bulk inserts using UNION SELECT, but it doesn’t seem to be happy unless I specify an id for each row:
sqlite> create table CelestialObject (id INTEGER PRIMARY KEY, name VARCHAR(25), distance REAL);
sqlite> insert into CelestialObject select 'Betelguese' as name, 200 as distance UNION SELECT 'Procyon', 500;
Error: table CelestialObject has 3 columns but 2 values were supplied
If I specify AUTOINCREMENT for the id (i.e., “id INTEGER PRIMARY KEY AUTOINCREMENT”) when I create the table, the error is the same.
Can anyone tell me whether there’s a way to use a bulk insert without specifying an id for each row?
Either provide three values per row:
or explicitly specify the columns you want to fill:
If you have SQLite 3.7.11 or later, you can do bulk inserts easier: