dimanche 2 août 2015

Android Sqlite: Delete first n records in a table (is deleting all rows instead)

I am using the following sql statement to delete the first 5 rows in my table:

DELETE FROM tblname WHERE `id`   IN (SELECT `id`   FROM tblname ORDER BY `id`   ASC LIMIT 5)

Instead of deleting 5 rows it deletes all of the rows.

I have also tried the following variant:

DELETE FROM tblname WHERE 'rowid' IN (SELECT 'rowid' FROM tblname ORDER BY 'rowid' ASC LIMIT ?)

I can't find any errors being generated by this. Here is the specific code I am using to perform this operation:

final SQLiteDatabase db = getWritableDatabase();
final String selectQuery = "DELETE FROM " + TABLE_NAME + " WHERE '_id' IN (SELECT '_id' FROM "+ TABLE_NAME + " ORDER BY '_id' ASC LIMIT 5)";

final Cursor cursor = db.rawQuery(selectQuery, null);

cursor.moveToFirst();
cursor.close();

Any pointers would be be much appreciated.

Aucun commentaire:

Enregistrer un commentaire