Android SQLite Query - Getting latest 10 records

后端 未结 9 925
死守一世寂寞
死守一世寂寞 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:29

    Change the DESC to ASC and you will get the records that you want, but if you need them ordered, then you will need to reverse the order that they come in. You can either do that in your own code or simply extend your query like so:

    select * from (
        select *
        from tblmessage
        order by sortfield ASC
        limit 10
    ) order by sortfield DESC;
    

    You really should always specify an order by clause, not just ASC or DESC.

提交回复
热议问题