I have the following SQLite code. How do I insert an auto generating unique ID into each row?
tx.executeSql('DROP TABLE IF EXISTS ENTRIES');
tx.executeSql('CREATE TABLE IF NOT EXISTS ENTRIES (id unique, data)');
tx.executeSql('INSERT INTO ENTRIES (id, data) VALUES (1, "First row")');
tx.executeSql('INSERT INTO ENTRIES (id, data) VALUES (2, "Second row")');
This is the syntax that I use.
Simply don't provide data for the autoincrement column
Or even simpler
for the INSERT, better provide a "null" value for the corresponding autoincrement value's question mark placeholder.
autoincrement
is your friend buddy.and then:
This worked perfectly for me
You could define
id
as an auto increment column:As MichaelDorner notes, the SQLite documentation says that an
integer primary key
does the same thing and is slightly faster. A column of that type is an alias forrowid
, which behaves like an autoincrement column.This behavior is implicit and could catch inexperienced SQLite developers off-guard.