xref: /netbsd-src/external/public-domain/sqlite/man/sqlite3_last_insert_rowid.3 (revision b9988867a8ad969c45a52aa7628bc932ec98d46b)
1.Dd January 24, 2024
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.In sqlite3.h
9.Ft sqlite3_int64
10.Fo sqlite3_last_insert_rowid
11.Fa "sqlite3*"
12.Fc
13.Sh DESCRIPTION
14Each entry in most SQLite tables (except for WITHOUT ROWID
15tables) has a unique 64-bit signed integer key called the "rowid".
16The rowid is always available as an undeclared column named ROWID,
17OID, or _ROWID_ as long as those names are not also used by explicitly
18declared columns.
19If the table has a column of type INTEGER PRIMARY KEY
20then that column is another alias for the rowid.
21.Pp
22The sqlite3_last_insert_rowid(D) interface usually returns the rowid
23of the most recent successful INSERT into a rowid table or virtual table
24on database connection D.
25Inserts into WITHOUT ROWID tables are not recorded.
26If no successful INSERTs into rowid tables have ever occurred
27on the database connection D, then sqlite3_last_insert_rowid(D) returns
28zero.
29.Pp
30As well as being set automatically as rows are inserted into database
31tables, the value returned by this function may be set explicitly by
32.Fn sqlite3_set_last_insert_rowid
33Some virtual table implementations may INSERT rows into rowid tables
34as part of committing a transaction (e.g. to flush data accumulated
35in 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
40.Fn sqlite3_set_last_insert_rowid
41before returning 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
66.Fn sqlite3_last_insert_rowid
67function is running and thus changes the last insert rowid, then
68the value returned by
69.Fn sqlite3_last_insert_rowid
70is unpredictable and might not equal either the old or the new last
71insert rowid.
72.Sh IMPLEMENTATION NOTES
73These declarations were extracted from the
74interface documentation at line 2527.
75.Bd -literal
76SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
77.Ed
78.Sh SEE ALSO
79.Xr sqlite3_set_last_insert_rowid 3
80