Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting specified number of rows from SQLite database

Tags:

android

sqlite

I am trying to remove 6 rows from the database using the following statement but I get the error shown below.

getWritableDatabase().execSQL("DELETE FROM tblname ORDER BY _id ASC LIMIT 6;");

Error:

Caused by: android.database.sqlite.SQLiteException: near "ORDER": syntax error: DELETE FROM tblname*

I tried reformatting the SQL in different ways but couldn't get it to work. What am I missing?

like image 840
Satish Avatar asked Aug 09 '10 01:08

Satish


2 Answers

DELETE FROM tblname WHERE `_id` IN (SELECT `_id` FROM tblname ORDER BY `_id` ASC LIMIT 6)

I think your problem may have been quoting the _id, though.

like image 158
Borealid Avatar answered Nov 15 '22 11:11

Borealid


The LIMIT and ORDER options for DELETE in sqlite are optional, and it appears they aren't enabled on Android. Borealid's SQL above will work fine even without the quotes.

like image 39
Robobunny Avatar answered Nov 15 '22 12:11

Robobunny