Android SQLite Query - Getting latest 10 records

后端 未结 9 923
死守一世寂寞
死守一世寂寞 2020-12-05 04:56

I have a database saved in my Android application and want to retrieve the last 10 messages inserted into the DB.

When I use:

Select * from tblmessag         


        
9条回答
  •  误落风尘
    2020-12-05 05:34

    If your table contains a column with primary key autoincrement (some "row_id" for example) then you just need single select with DESC order by this column

    Raw request looks like

    select * from table_name order by row_id DESC limit 10
    

    Android implementation is

    private Cursor queryLastEvents() {
        return getDatabase().query("table_name", null, null, null, null, null, "row_id DESC", "10");
    }
    

提交回复
热议问题