Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete sqlite database when updating new version of application

I have uploaded an apk (version 1.0) with 22 tables in SQLite Database on Google Playstore.

Now i want to update database with 36 tables in new version (version 2.0) of application.

I am storing datebase at default location so when i press "Clear data" in Application Manager, database is gonna deleted.

I just want to know how to delete old database (same as clear data) when user update new version?

Update:

If is there any solution for clear data while updating application from play store then also answered me.

Your help would be appreciated.

Thanks.

like image 655
Pratik Butani Avatar asked May 27 '14 04:05

Pratik Butani


Video Answer


2 Answers

Finally done with simple solution:

/** UPGRADE DATABASE **/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    Log.i(TAG, "Database Version: OLD: "+ oldVersion + " = NEW: "+newVersion);

    if(context.deleteDatabase(DATABASE_NAME))
        Log.i(TAG, "Database Deleted....");
    else
        Log.i(TAG, "Database Not Deleted..");
}
like image 43
Pratik Butani Avatar answered Sep 22 '22 14:09

Pratik Butani


you can use this method to delete your database.

context.deleteDatabase(DATABASE_NAME);

You can also use this method to find your database path first and then delete that.

File myDb = context.getDatabasePath(DATABSE_NAME);
Boolean isDelete = myDb.delete();

The other solution is , if you want to update your database then just change your version number of database. onUpgrade() will automatically get called and your old database will be deleted and new database will be created.

like image 171
Waqar Ahmed Avatar answered Sep 24 '22 14:09

Waqar Ahmed