delete sqlite database when updating new version of application

14,463

Solution 1

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..");
}

Solution 2

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.

Solution 3

Nice Question. Just follow the steps.

1) There are two methods that you override on Your Helper class

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if(oldVersion!=newVersion){
        // Remove all old tables and crate new ones
    }
}

2) create an object of Helper class.

MyOpenHelper dbHelper = new MyOpenHelper(this,"dbname", 1);

3) Now just increase the database version when you want to change the database.(Third argument in MyOpenHelper class. "1" is a database version).

4) When database version changes it will give you a callback in onUograde() method.

5) Remove all tables in onUpgrade() method and create new ones.

6) That's it.

EXPLANATION :

Your app version 1.1 is on google play and your database version is "1". Now you want to upload version 1.2 with new database. Just set database version "2" in your helper file. And check the the oldVersion and newVersion in onUpgrade() method if both are not same that means application updating and remove old tables and create new ones.

Solution 4

If you are using SQLiteOpenHelper just delete your tables in your onUpgrade method and recreate all your tables again. You should have not problems at all.

Check this out.

Hope it helps

Note: Be careful with database versions, if you are playing with your database numbers make sure you have the right number as your oldVersion or it will not work properly when you update your google play.

Share:
14,463
Pratik Butani
Author by

Pratik Butani

Pratik Butani is Android/Flutter Lead at 7Span - Ahmedabad. He is working with Flutter Development since 2020 and Android Development since 2013. He is on the list of Top 100 User’s (85th) in India and Top 10 User’s (6th) in Gujarat as Highest Reputation Holder on StackOverflow. He was co-organizer at Google Developer Group – Rajkot in 2014-17. All-time Learner of new things, Googler and Eager to Help IT Peoples. He is also likes to share his knowledge of Android and Flutter with New Learner. SOReadyToHelp ツ Fell in Love with Android ツ I really feel proud to up-vote My Favorite #SO friend. => Android Application Developer by Passion :) => Being Helpful by Nature ;) => Now in List of Top 100 User's (85) in India and Top 10 User's (6) in Gujarat as Highest Reputation Holder on StackOverflow => Visit my blogs for learning new things : Happy to Help :) :) => My Apps on Playstore: AndroidButs & PratikButani => My Articles on Medium => Join Me on LinkedIn => Tweet Me on Twitter => Follow Me on Quora - Top Writer on Quora in 2017 => Hangout with Me on [email protected] => Social Networking Facebook => Get Users list whose bio's contains given keywords More about me :) -> Pratik Butani

Updated on June 07, 2022

Comments

  • Pratik Butani
    Pratik Butani almost 2 years

    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.