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")');
You could define
idas an auto increment column:As MichaelDorner notes, the SQLite documentation says that an
integer primary keydoes almost the same thing and is slightly faster. A column of that type is an alias forROWID, which behaves like an autoincrement column with the difference that withoutAUTOINCREMENTthe ID of the row might be reused. "In other words, the purpose ofAUTOINCREMENTis to prevent the reuse of ROWIDs from previously deleted rows."[source]This behavior is implicit and could catch inexperienced SQLite developers off-guard.