sqliteHow do I retrieve the last insert ID in SQLite?
The last_insert_rowid()
function can be used to retrieve the last insert ID in SQLite. This function is used to retrieve the rowid of the last row insert from the database connection which invoked the function.
Example code
INSERT INTO contacts (firstname, lastname) VALUES ('John', 'Doe');
SELECT last_insert_rowid();
Output example
1
The code above inserts a new row into the contacts
table and then retrieves the rowid of the last row insert. In this case, the rowid is 1
.
Code explanation
INSERT INTO
: this is a SQL statement used to insert a row into a tablecontacts
: this is the name of the tablefirstname
andlastname
: these are the column names of the tableVALUES
: this is a keyword used to specify the values to be insertedJohn
andDoe
: these are the values to be insertedSELECT
: this is a SQL statement used to select data from a tablelast_insert_rowid()
: this is the function used to retrieve the last insert ID
Helpful links
More of Sqlite
- How do I extract the year from a datetime value in SQLite?
- How do I format a date in SQLite using the YYYYMMDD format?
- How do I use SQLite xfilter to filter data?
- How do I decide between using SQLite and MySQL for my software development project?
- How do I use a SQLite viewer to view my database?
- How do I use the SQLite sequence feature?
- How do I use the SQLite ZIP VFS to compress a database?
- How do I use SQLite Studio to manage my database?
- How do I call sqlitepcl.raw.setprovider() when using SQLite?
- How to configure SQLite with XAMPP on Windows?
See more codes...