简体   繁体   中英

Android SQLite delete table not actually deleting the rows

Hi i am trying to delete all the rows in a table by below query

db.beginTransaction(); // db is a SQLiteDatabase object
int deleted = db.delete("user", null, null);
db.endTransaction();

I am getting n number of rows deleted in the table. But still rows exist in the table. Anything wrong in my delete call?

You need to call setTransactionSuccessful to commit all the changes done to the database, before calling endTransaction :

db.beginTransaction();
int deleted = db.delete("user", null, null);
db.setTransactionSuccessful();
db.endTransaction();

Source (Android developer reference):

The changes will be rolled back if any transaction is ended without being marked as clean (by calling setTransactionSuccessful). Otherwise they will be committed.

Just try this

public int delete(String tableName) {
    SQLiteDatabase db = this.getWritableDatabase();
    int x = db.delete(tableName, null, null);
    db.close();
    return x;
}

Try it :

db.execSQL("DROP TABLE IF EXISTS table_name");

Where db is a reference to a SqliteDatabase object.

When we install the app the database file is recreated in the /data folder. The problem may occour due to recreation of database even after deleting the files.

Check if this is the case.

String query="delete from "+tablename;

SQLiteDatabase db=getWritableDatabase();

db.execSQL(query);

Log.d("msg","deleted");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM