delete sqlite database when updating new version of application

蓝咒 提交于 2019-12-20 10:27:42

问题


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.


回答1:


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.




回答2:


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



回答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.




回答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.




回答5:


The obvious solution is to delete the old DB file when onUpgrade() is called and copy the new version over. Too bad it does not work. You get an error something like this:

Caused by: android.database.sqlite.SQLiteReadOnlyDatabaseException: attempt to write a readonly database (code 1032)

SQLiteLog﹕ (1032) statement aborts at 4: [PRAGMA user_version = 2] SQLiteLog﹕ (28) file unlinked while open: /data/data/my.package/databases/my_db.db

You don't need to delete the database, just copy over it using the method you've alady defined (copyDataBase), like this:

@Override  
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
    Toast.makeText(myContext, "onUpgrade called!", Toast.LENGTH_LONG).show();  
    if (oldVersion < newVersion) {  
        Log.v("Database Upgrade", "Database version higher, upgrading");  
        try { 
            copyDataBase(); 
        } catch (IOException e) { 
            throw new Error("Error upgrading database"); 
        } 
    } 
}  

wrok for me, thank



来源:https://stackoverflow.com/questions/23881317/delete-sqlite-database-when-updating-new-version-of-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!