user3443877
user3443877

Reputation: 51

Remove old row from SQLite

We use databases on Android. We collect a lot of data in them so as not to clutter the memory of the user, we want to do cleaning. In the database we want to keep 35 latest records. How to do it?

Upvotes: 0

Views: 133

Answers (3)

zozelfelfo
zozelfelfo

Reputation: 3776

Without providing more information I can give you a raw SQL, because I don't know how you have implemented your SQLite database.

DELETE FROM my_table WHERE my_id NOT IN(SELECT my_id FROM my_table ORDER BY my_id DESC LIMIT 35) this way you will delete all the registers but the 35 most recent (supposing you have an incremented primary key in your table.)

Hope it helps.

Upvotes: 1

icastell
icastell

Reputation: 3605

I think you have to concat 'LIMIT 35' to your where clause. Like this answer: https://stackoverflow.com/a/7700422/1143172.

I mean:

DELETE FROM [tbl_names] LIMIT 35

Upvotes: 0

somerandomusername
somerandomusername

Reputation: 2023

You have to think in what way you can sort your table to get latest 35 records. Maybe it's id or date field. You should check this post for more Delete all but top n from database table in SQL

Upvotes: 0

Related Questions