Handy
Handy

Reputation: 99

Ask, how to delete all data on sqlite

i want to make "clear-all" button which delete all data in my sqlite, my code is like this :

private void clearAll() {

        mDbHelper = new NotesDbAdapter(this);
        mDbHelper.open();
        Cursor notesCursor = mDbHelper.fetchAllNotes();
        notesCursor.moveToFirst();
        do{
        for(int i=0;i<notesCursor.getCount();i++){
            mDbHelper.deleteNote(i);
        }
        }while(notesCursor.moveToNext());
}

but i can't delete it, anyone can help me to solve this problem? thanks before

Upvotes: 0

Views: 634

Answers (2)

Steve Bergamini
Steve Bergamini

Reputation: 14600

It appears that you're getting the count of your "notes" table in order to pass in to use as the ID for the delete statement. Why? How can you guarantee that the IDs will match up? This is especially true after subsequent deletes. Why not just call the SQLiteDatabase delete() method without a specific ID to delete all the records?

Upvotes: 0

FoamyGuy
FoamyGuy

Reputation: 46856

you should be able to use something like this:

mDbHelper.delete("tableName",null,null);
notesCursor.requery();

To delete all the values in your table and update your cursor.

Upvotes: 1

Related Questions