Page 1 of 1

Get last_insert_id SQLite?

Posted: Fri 27 Feb 2015 15:32
by Grolle
Hi,

how can I receive the last_insert_id with UniCommand and SQLite?

Best regards ...

Re: Get last_insert_id SQLite?

Posted: Mon 02 Mar 2015 15:36
by Pinturiccio
If we understood you correctly, you want to get the latest inserted rowid. There can be two possible cases:
1. A non-autoincrement primary key column is used in your table. In this case you know the primary key value of the inserted column, and you can get the last inserted rowid with the following query:

Code: Select all

select rowid from table where id = :id
Where :id is a parameter containing the primary key value of the inserted row.

2. If an autoincrement primary key column is used in your table, you need to get the autoincrement column value of the last inserted row first. You can do it with the following query:

Code: Select all

"SELECT seq from sqlite_sequence WHERE lower(name)='<table_name>'"
Then perform the query for the previous case:

Code: Select all

select rowid from table where id = :id