Check for previous and next entry in SQLITE
I have an android application that uses a sqlite database. M开发者_如何学Goy app has a next and previous button to view entries in the database. I need to check whether or not a next or previous entry exists that way i can disable the button if one does not exist.
Run a query to get the IDs of each record in the table and cache this. Then you know if there are next or previous entries. Click the button, and your app would do a primary key lookup based on the cached data.
Try something like this:
SELECT *,
(SELECT id FROM my_table WHERE id < <my_id> ORDER BY id DESC LIMIT 1) AS previous_id,
(SELECT id FROM my_table WHERE id > <my_id> ORDER BY id ASC LIMIT 1) AS next_id
FROM my_table WHERE a = <my_id>
精彩评论