Get last_insert_id SQLite?

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for universal data access
Post Reply
Grolle
Posts: 13
Joined: Tue 15 Apr 2008 12:02

Get last_insert_id SQLite?

Post by Grolle » Fri 27 Feb 2015 15:32

Hi,

how can I receive the last_insert_id with UniCommand and SQLite?

Best regards ...

Pinturiccio
Devart Team
Posts: 2125
Joined: Wed 02 Nov 2011 09:44

Re: Get last_insert_id SQLite?

Post by Pinturiccio » Mon 02 Mar 2015 15:36

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

Post Reply