Re: [sqlite] Shouldn't have to specify primary key explicitly
On 28 Jun 2018, at 12:48pm, Scott Robertson wrote: > CREATE TABLE test2 ( > id INTEGER PRIMARY KEY, > book text, > page INTEGER > ); > > INSERT INTO test2 VALUES ('Lord of the Rings', 327); > > Error: table test2 has 3 columns but 2 values were supplied > > INSERT INTO test2 VALUES (9, 'Lord of the Rings', 327); In the first example you declared a three-column table but supplied two values. In the second example you explicitly stated that you wanted to supply values for all the declared columns, so SQLite used the values you supplied. To avoid this do either of the following: INSERT INTO test2 VALUES (NULL, 'Lord of the Rings', 327); INSERT INTO test2 (book, page) VALUES ('Lord of the Rings', 327); Simon. ___ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Shouldn't have to specify primary key explicitly
>INTEGER PRIMARY KEY doesn’t default to autoincrement. It’s used in place of the automatically created >autoincrement rowid but you have to supply the values (I.e. they’re not created automatically). I stand corrected. If you supply null for the integer primary key it will assign the highest rowid + 1. -- Sent from: http://sqlite.1065341.n5.nabble.com/ ___ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Shouldn't have to specify primary key explicitly
INTEGER PRIMARY KEY doesn’t default to autoincrement. It’s used in place of the automatically created autoincrement rowid but you have to supply the values (I.e. they’re not created automatically). On 28 Jun 2018, at 12:48, Scott Robertson wrote: > SQLite is supposed to autoincrement by default when a column is defined > as "INTEGER PRIMARY KEY" according to everything I've read. But I've > only gotten this to work if I let SQLite create its own PK column. If I > have an explicit PK column, I am expected to specify an ID myself. What > am I missing? I don't know why I'm getting this error. Thanks. You've defined the table with three cols so you have to provide three values unless you name the cols you wish to fill. To get SQLite to auto increment, use NULL as the value fo your id column: INSERT INTO test2 VALUES (NULL, 'Fletch', 245); -- Cheers -- Tim ___ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users ___ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Re: [sqlite] Shouldn't have to specify primary key explicitly
On 28 Jun 2018, at 12:48, Scott Robertson wrote: > SQLite is supposed to autoincrement by default when a column is defined > as "INTEGER PRIMARY KEY" according to everything I've read. But I've > only gotten this to work if I let SQLite create its own PK column. If I > have an explicit PK column, I am expected to specify an ID myself. What > am I missing? I don't know why I'm getting this error. Thanks. You've defined the table with three cols so you have to provide three values unless you name the cols you wish to fill. To get SQLite to auto increment, use NULL as the value fo your id column: INSERT INTO test2 VALUES (NULL, 'Fletch', 245); -- Cheers -- Tim ___ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users