Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping SQLite data after update

Tags:

android

sqlite

I have an app that has an sqlite with 3 tables. My concern is that, if I introduce an update that adds another table (so that's 4 tables) then the updated version will wipe out the database.

How can I backup/restore db given that the backup happens before the update and the restore after the update? If I do it using the IO (copy to SD card and copy back) then it will fail.

I am thinking probably of exporting data to xml and loading manually. Is there another way? any example on how to do it?

like image 404
Snake Avatar asked Nov 24 '12 02:11

Snake


1 Answers

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

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

Technically you don't even need to increment the database version, anytime you have access to a copy of db you can execute your CREATE statement.

like image 84
Sam Avatar answered Oct 14 '22 02:10

Sam