1

I'd need your help, I'm starting with Android databases and I have a litte problem/question for update my tables (maybe is a stupid question but is hard for me ) :

I have a table with info about videos, one of the columns is the timestamp for indentify how old is the video. I'd like to delete all rows except the top 10 with higher timestamp.

if the table name is TABLE and the (important) rows are KEY_ID, and KEY_TIMESTAMP:

How can I concat the query1:

SELECT KEY_ID FROM TABLE ORDER BY KEY_TIMESTAMP ASC LIMIT (## dont know what to put here ##)

with the query

DELETE FROM TABLE WHERE KEY_ID = query1

in Android SQLite code? what should I put in between the ## ##

1

1 Answer 1

2

Try using DELETE FROM with a nested SELECT.

Not tested:

DELETE 
FROM TABLE
WHERE KEY_ID NOT IN
(
    SELECT TOP 10 KEY_ID 
        FROM TABLE 
        ORDER BY KEY_TIMESTAMP ASC
        LIMIT 10
)
Sign up to request clarification or add additional context in comments.

5 Comments

Oh thats much better solution! thanks a lot! Could you explain me how to concat both queries in android?
Not quite sure what you mean by concatenate. The query I provided should have the functionality you were explaining without the need to add anything else to it.
what i mean is: db.delete(TABLE, KEY_ID+" NOT IN "+ ##something##) or should I use execSQL (String sql) ? thanks
It might be easier for you to declare the query as a String and then call database.execSQL() with the query.
No problem! Good luck to you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.