db:last_insert_rowid

Returns the key of the most recent insert into the database

Prototype

db:last_insert_rowid()

Description

This function returns the rowid of the most recent INSERT into the database. If no inserts have ever occurred, 0 is returned. (Each row in an SQLite table has a unique 64-bit signed integer key called the 'rowid'. This id is always available as an undeclared column named ROWID, OID, or _ROWID_. If the table has a column of type INTEGER PRIMARY KEY then that column is another alias for the rowid.)

If an INSERT occurs within a trigger, then the rowid of the inserted row is returned as long as the trigger is running. Once the trigger terminates, the value returned reverts to the last value inserted before the trigger fired.

This returns a Lua number if possible, otherwise if the key is too large to be stored in a number it returns a string.

Lua functions

Topics