Enquire about the process of deleting all data from an SQLite database

If you are using SQLiteOpenHelper, a possible solution is to close the database, delete the entire file, and recreate the database. This approach is convenient because you don’t have to update the code that resets the tables every time you add new ones to the schema. The issue at hand is that you want to replace all the current entries in one of your SQLite tables with new data once it becomes available to your Android app.


Question:

I am attempting to create a button that will clear all of the information within my SQLite database. The code that I currently have for this task appears as follows:

private void clearAll() {
        mDbHelper = new NotesDbAdapter(this);
        mDbHelper.open();
        Cursor notesCursor = mDbHelper.fetchAllNotes();
        notesCursor.moveToFirst();
        do{
        for(int i=0;i

Can somebody assist me in resolving this issue of being unable to delete it? Appreciate your assistance.


Solution 1:

An example of what can be used is something similar to this.

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

Clear all the values in your table and refresh your cursor.


Solution 2:


It seems that you’re retrieving the count of your “notes” table to serve as the ID for the delete statement. But have you considered how you can ensure that the IDs will align? This may be particularly difficult after further deletions. Alternatively, why not utilize the SQLiteDatabase delete() method without specifying a particular ID to remove all the records?

Frequently Asked Questions