Saving SQLite data after upgrade

I have an application with sqlite with 3 tables. I am worried that if I introduce an update that adds another table (so there are 4 tables), then the updated version will destroy the database.

How do I backup / restore db given that the backup occurs before the upgrade and recovery after the upgrade? If I do this using I / O (copy to SD card and copy it back), it will not work.

I think it is possible to export the data in xml and load manually. Is there another way? any example on how to do this?

+7
source share
1 answer

If you just want to add a new table, comment out the DROP TABLE commands in your onUpgrade() method. You have full control over the code in onUpgrade() , so it could just be:

 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("CREATE TABLE ..."); } 

Technically, you don’t even need to increase the version of the database, at any time when you have access to a copy of db , you can execute the CREATE statement.

+4
source

All Articles