SQLite Forum

How to get the last row_id in a table without inserting anything
Why do you think you need to know the max(rowid)? Are you implying that numerical order of rowids == chronological order of insertion? It would take AUTOINCREMENT to guarantee that SQLite does not re-use the rowids of deleted records; and even that does not guarantee that the rowids assigned are contigous. Some may be left out in special cases where a rowid is already assigned but the insert fails due to constraints.

If you need to retrieve rows in a particular order, you must provide an ORDER BY clause; not providing an ORDER BY clause leaves SQLite the freedom to provide rows in any order it chooses (i.e. visitation oder), which may change if the query plan changes (typically after running analyse when the "shape" of your data has changed or upgrading SQLite to a newer/better query planner).

To iterate of all rows in descending order of rowid, use


there is no need to manually iterate rowids.