Skip to content Skip to sidebar Skip to footer

Sqlite: Order By 'insertorderindb'

i need to know how to make a select with the insert order in DB without making any index field by my own . May be this posible? And , the insertion order in a transaction is linear

Solution 1:

SQLite tables have a "secret" column called ROWID the value of which increases as you add rows. It can serve as a proxy for "order entered into table". ROWID is basically a "free" auto-incrementing integer primary key column for the table.

Note that by definition SQL databases have no notion of row ordering. While I would be surprised if the behavior of ROWID every changed in SQLite (since it's existence and behavior is documented), as a rule if you want to retrieve rows in an ordered fashion in SQL databases you're responsible for storing the ordering information yourself.

Solution 2:

orderby _id

should do, given the autoincrement nature of the id.

Solution 3:

The best-practices method of sorting rows by the temporal order in which they have been inserted is to use a timestamp column which is assigned as a default value the date-time corresponding to "now", as the row is being inserted. This is easy to do in SQLite.

sqlite database default time value 'now'

Post a Comment for "Sqlite: Order By 'insertorderindb'"