Delete sqlite database when updating a new version of an application

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

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

I save datebaseby default, so when I click "Clear data in Application Manager , the database is deleted.

I just want to know how to delete old databases (same as clear data) when a user updates a new version?

Update:

If there is any solution for clear data when updating the application from the playback store, then also answered me.

Your help will be appreciated.

Thank.

+4
source share
4 answers

Finally, with a 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..");
}
+2
source

You can use this method to delete your database.

context.deleteDatabase(DATABASE_NAME);

You can also use this method to first find the path to the database and then delete it.

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

Another solution is if you want to update your database, just change your database version number. onUpgrade()will be automatically called and your old database will be deleted and a new database will be created.

+5
source

SQLiteOpenHelper, onUpgrade . .

.

,

. , , , oldVersion, Google.

+1

. .

1) , ""

@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) Helper.

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

3) database version, . ( MyOpenHelper. "1" - ).

4) onUograde().

5) onUpgrade() .

6) .

:

version 1.1 Google database version is "1". 1.2 . "2" . oldVersion newVersion onUpgrade(), , .

+1
source

Source: https://habr.com/ru/post/1542120/


All Articles