How can I get last autoincrement value of specific table right after I open database? It’s not last_insert_rowid() because there is no insertion transaction. In other words I want to know in advance which number autoincrement will choose when inserting new row for particular table.
How can I get last autoincrement value of specific table right after I open
Share
It depends on how the autoincremented column has been defined.
If the column definition is
INTEGER PRIMARY KEY AUTOINCREMENT, then SQLite will keep the largest ID in an internal table calledsqlite_sequence.If the column definition does NOT contain the keyword
AUTOINCREMENT, SQLite will use its ‘regular’ routine to determine the new ID. From the documentation:I remember reading that, for columns without
AUTOINCREMENT, the only surefire way to determine the next ID is to VACUUM the database first; that will reset all ID counters to the largest existing ID for that table + 1. But I can’t find that quote anymore, so this may no longer be true.That said, I agree with slash_rick_dot that fetching auto-incremented IDs beforehand is a bad idea, especially if there’s even a remote chance that another process might write to the database at the same time.