1.Dd December 19, 2018 2.Dt SQLITE3_LAST_INSERT_ROWID 3 3.Os 4.Sh NAME 5.Nm sqlite3_last_insert_rowid 6.Nd Last Insert Rowid 7.Sh SYNOPSIS 8.Ft sqlite3_int64 9.Fo sqlite3_last_insert_rowid 10.Fa "sqlite3*" 11.Fc 12.Sh DESCRIPTION 13Each entry in most SQLite tables (except for WITHOUT ROWID 14tables) has a unique 64-bit signed integer key called the "rowid". 15The rowid is always available as an undeclared column named ROWID, 16OID, or _ROWID_ as long as those names are not also used by explicitly 17declared columns. 18If the table has a column of type INTEGER PRIMARY KEY 19then that column is another alias for the rowid. 20.Pp 21The sqlite3_last_insert_rowid(D) interface usually returns the rowid 22of the most recent successful INSERT into a rowid table or virtual table 23on database connection D. 24Inserts into WITHOUT ROWID tables are not recorded. 25If no successful INSERTs into rowid tables have ever occurred 26on the database connection D, then sqlite3_last_insert_rowid(D) returns 27zero. 28.Pp 29As well as being set automatically as rows are inserted into database 30tables, the value returned by this function may be set explicitly by 31sqlite3_set_last_insert_rowid() 32.Pp 33Some virtual table implementations may INSERT rows into rowid tables 34as part of committing a transaction (e.g. 35to flush data accumulated in memory to disk). 36In this case subsequent calls to this function return the rowid associated 37with these internal INSERT operations, which leads to unintuitive results. 38Virtual table implementations that do write to rowid tables in this 39way can avoid this problem by restoring the original rowid value using 40sqlite3_set_last_insert_rowid() before 41returning control to the user. 42.Pp 43If an INSERT occurs within a trigger then this routine will return 44the rowid of the inserted row as long as the trigger is running. 45Once the trigger program ends, the value returned by this routine reverts 46to what it was before the trigger was fired. 47.Pp 48An INSERT that fails due to a constraint violation is not a successful 49INSERT and does not change the value returned by this routine. 50Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK, and INSERT 51OR ABORT make no changes to the return value of this routine when their 52insertion fails. 53When INSERT OR REPLACE encounters a constraint violation, it does not 54fail. 55The INSERT continues to completion after deleting rows that caused 56the constraint problem so INSERT OR REPLACE will always change the 57return value of this interface. 58.Pp 59For the purposes of this routine, an INSERT is considered to 60be successful even if it is subsequently rolled back. 61.Pp 62This function is accessible to SQL statements via the last_insert_rowid() SQL function. 63.Pp 64If a separate thread performs a new INSERT on the same database 65connection while the sqlite3_last_insert_rowid() 66function is running and thus changes the last insert rowid, then 67the value returned by sqlite3_last_insert_rowid() 68is unpredictable and might not equal either the old or the new last 69insert rowid. 70.Sh SEE ALSO 71.Xr sqlite3_last_insert_rowid 3 , 72.Xr sqlite3_set_last_insert_rowid 3 73