How to get the primary key (for example, ID) from simply inserted into SQLite DB in the most efficient way?


for instance: a table created as below:

create table tbl_text( id integer primary key, txt text unique, ref integer );

insert a record:

insert into tbl_text values(null, "Hello", 123);

how to get the id of "Hello" just inserted in fastest way? query works but it might be time-consuming, e.g.

select id from tbl_text where txt='Hello'

You can use last_insert_rowid() or sqlite3_last_insert_rowid() depending on how you're accessing sqlite. Watch out for another thread running an insert on the same connection, more about that in their documentation