0

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-mode? (first insert sentences -> first insert )

DB CREATION:

private static final String CREATE_TABLE_SONGS = "CREATE TABLE IF NOT EXISTS SONGS (" +
        "LIST_ID int NOT NULL, " +
        "PLAY_ID int NOT NULL, " + //ID of the song in the list
        "SONG_ID int NOT NULL, " + //ID of the song
        "TITLE varchar(200) NOT NULL," +
        "TIME int NOT NULL, " +
        "PROVIDER int NOT NULL, " +
        "PROVIDER_ID varchar(200) NOT NULL, " +
        "SONG_STATE int NOT NULL, " +
        "PRIMARY KEY (LIST_ID,PLAY_ID));";

And then

db.execSQL(CREATE_TABLE_SONGS);
A.Quiroga
  • 5,704
  • 6
  • 37
  • 58

3 Answers3

1
order by _id

should do, given the autoincrement nature of the id.

njzk2
  • 38,969
  • 7
  • 69
  • 107
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.

Larry Lustig
  • 49,320
  • 14
  • 110
  • 160
0

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'

Community
  • 1
  • 1
Tim
  • 5,371
  • 3
  • 32
  • 41