Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Delete All Items From SQLite in Android

Tags:

I would like to make an app where the user clicks a button, and the SQLite database is cleared. Here's what I've tried so far:

db.delete(TABLE_NAME, null, null); 

What am I doing wrong?

like image 333
Cg2916 Avatar asked Apr 16 '11 01:04

Cg2916


2 Answers

Your delete() call is correct. Did you get writable database? Here is example of method using delete:

/**  * Remove all users and groups from database.  */ public void removeAll() {     // db.delete(String tableName, String whereClause, String[] whereArgs);     // If whereClause is null, it will delete all rows.     SQLiteDatabase db = helper.getWritableDatabase(); // helper is object extends SQLiteOpenHelper     db.delete(DatabaseHelper.TAB_USERS, null, null);     db.delete(DatabaseHelper.TAB_USERS_GROUP, null, null); } 
like image 160
petrnohejl Avatar answered Oct 16 '22 02:10

petrnohejl


db.delete(TABLE_NAME, null, null); is the correct syntax to delete all rows in a table. But I think you would have given table name directly without enclosing it in double-quotes. Try like this

db.delete("TABLE_NAME", null, null); 

It might help :)

like image 29
suraj Avatar answered Oct 16 '22 03:10

suraj