Home Contents | # sql:lastInsertId | Sql | Core4 Lua Commands

## SYNOPSIS

id = query:lastInsertId()

## DESCRIPTION

Each entry in an SQLite table has a unique 64-bit signed integer key called the "rowid". The rowid is always available as an undeclared column named ROWID, OID, or _ROWID_ as long as those names are not also used by explicitly declared columns. If the table has a column of type INTEGER PRIMARY KEY then that column is another alias for the rowid.

This routine returns the rowid of the most recent successful INSERT into the database from the database connection in the first argument. If no successful INSERTs have ever occurred on that database connection, zero is returned.

## RETURN VALUE

If the rowid fits into a 32-bit signed integer, a number is returned. If this is not possible, the number is returned as a string. This behaviour stems from the fact that the rowid is 64-bits, but Lua on Core4 works on the native ARM9 word size of 32-bit.