Name

sqlite3_last_insert_rowid() — Get the last inserted ROWID

Definition

sqlite3_int64 sqlite3_last_insert_rowid( sqlite3* db );
db

A database connection.

Returns

The value of the last inserted ROWID.

Description

This function returns the ROWID of the last successfully inserted row. If no rows have been inserted since the database connection was opened, this function will return zero (0). Note that zero is a valid ROWID, but it will never be automatically assigned by SQLite. This function is typically used to get the automatically generated value for a newly inserted record. The value is often used to populate foreign keys.

If the INSERT happens inside of a trigger, the inserted ROWID value is valid for the duration of the trigger. Once the trigger exits, the value returns to its previous value.

If the SQLite library is in “serialized” threading mode, there is a risk of a race condition between threads. To avoid problems, the current thread should use sqlite3_mutex_enter() to acquire exclusive access to the database connection before the initial API call is made. The thread can release the mutex after sqlite3_last_insert_rowid() is called. In “multithread” mode, it is the responsibility of the application to control access to the database connection.

This function is exposed to the SQL environment as the SQL function last_insert_rowid().

See Also

last_insert_rowid() [SQL Func, Ap E]

Get Using SQLite now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.