How to programmatically drop Android Sqlite database

I am trying to provide an option in my application for the user to erase all data and start over. I've tried a few things, but none works in the sense that the data still exists. Here's my first try:

public void clearDatabase(Context context) {
        DatabaseHelper helper = new DatabaseHelper(context);
        SQLiteDatabase database = helper.getWritableDatabase();
        database.delete(Constants.TRANSACTION_TABLE, null, null); //erases everything in the table.
        database.delete(Constants.CUSTOMER_TABLE, null, null);
        database.delete(Constants.RETAILER_TABLE, null, null);

        database.close();
    }

      

This seems to work, but then the app will restart.

Then I try this from subclass SQliteOpenHelper

  public void resetDatabase() {
    SQLiteDatabase database = getWritableDatabase();
    database.execSQL("DROP TABLE IF EXISTS customer_table");
    database.execSQL("DROP TABLE IF EXISTS transaction_table");
    database.execSQL("DROP TABLE IF EXISTS retailer_table");
    database.execSQL(CREATE_CUSTOMER_TABLE);
    database.execSQL(CREATE_TRANSACTION_TABLE);
    database.execSQL(CREATE_RETAILER_TABLE);
    database.close();
}

      

This is not a crash on restart, but the data still exists. The only thing that worked was to change the version code programmatically. But then when I run the app from the IDE it will have an older version number and the app won't start because the version number on the device is higher than the version number in the code.

What else can I try?

+3


source to share


3 answers


Finally it worked for me

First uncheck and create the database

  public void resetDatabase() {
        SQLiteDatabase database = getWritableDatabase();
        database.execSQL("DROP TABLE IF EXISTS " + Constants.CUSTOMER_TABLE);
        database.execSQL("DROP TABLE IF EXISTS " + Constants.TRANSACTION_TABLE);
        database.execSQL("DROP TABLE IF EXISTS " + Constants.RETAILER_TABLE);
        database.execSQL(CREATE_CUSTOMER_TABLE);
        database.execSQL(CREATE_TRANSACTION_TABLE);
        database.execSQL(CREATE_RETAILER_TABLE);
        database.close();
    }

      



And then restart the application borrowed from this question how to programmatically restart "Android application?"

public static void resetApp() {
        sDatabase.resetDatabase(); 
        preferenceEditor.clear();
        preferenceEditor.commit();
        Intent mStartActivity = new Intent(sContext, MainActivity.class);
        int mPendingIntentId = 123456;
        PendingIntent mPendingIntent = PendingIntent.getActivity(sContext, mPendingIntentId,    mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager mgr = (AlarmManager)sContext.getSystemService(Context.ALARM_SERVICE);
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
        System.exit(0);

    }

      

+5


source


Try this code



    SQLiteDatabase db = dbHelper.getWritableDatabase(); 

    count = db.delete("table_name", selection, selectionArgs);

      

+1


source


I think the best and easiest way to delete a complete DB file is context.deleteDatabase(DATABASE_NAME);

but before doing that, you need to close all your DB connection to restart the application. Hope this helps you.

+1


source







All Articles