Skip to content Skip to sidebar Skip to footer

Deleting From Android Sqlite Database

Question I am trying to use a button to delete the first entry to my database. I thought I had the code right but it does not seem to be deleting any of the data, when the delete

Solution 1:

The first record in the database will not always have an ID of "0". Each time you insert a new record the primary key will increment by 1. So what you need is to get the id of the first record, and perform the delete using that id.

Cursor cursor = db.query(DATABASE_TABLE, null, null, null, null, null, null); 

    if(cursor.moveToFirst()) {
        long rowId = cursor.getLong(cursor.getColumnIndex(KEY_ROWID)); 

        db.delete(DATABASE_TABLE, KEY_ROWID +  "=" + rowId, null);
   }

Post a Comment for "Deleting From Android Sqlite Database"