I have some Entity to store my data into ROOM. How to store only 10 last row to my db using room. For now I'n using @Query("SELECT * FROM Entity LIMIT 10")
but it's dont looks right
Asked
Active
Viewed 3,600 times
1

Strangelove
- 761
- 1
- 7
- 29
-
Maybe with: `SELECT * FROM Entity ORDER BY Id DESC LIMIT 10`? – MatPag Jan 31 '19 at 08:44
-
Possible duplicate of [Limit number of records in a table in SQLite](https://stackoverflow.com/questions/31803682/limit-number-of-records-in-a-table-in-sqlite) – karan Jan 31 '19 at 08:46
2 Answers
1
We you want only first 10 or less records in your database then you have to set the id as auto increment and try to delete all those records where the ids doesn't match the first 10 results (after every insertion)
DELETE FROM tableName where id NOT IN (SELECT id from tableName ORDER BY id DESC LIMIT 10)
Here is a link to explore more:-

DeePanShu
- 1,236
- 10
- 23
0
Does it work?
This is the correct way to do it:
SELECT expressions
FROM tables
[WHERE conditions]
[ORDER BY expression [ ASC | DESC ]]
LIMIT number_rows [ OFFSET offset_value ];
And here's a real example:
SELECT contact_id, last_name, first_name
FROM contacts
WHERE website = 'TechOnTheNet.com'
ORDER BY contact_id ASC
LIMIT 5;
I got these from https://www.techonthenet.com/sql/select_limit.php.
There could by slight differences in syntax as I do not know which type of SQL you are using.

James
- 347
- 4
- 13