Home > Back-end >  Android - new column in database and restore DB from backup
Android - new column in database and restore DB from backup

Time:11-06

In Android I am using SQLite database and has a structure with ID, name, age.

The users have an ability to backup the database to Google Drive. Untill now everything is OK.

The problem is, I am developing a new version of my app and I am adding another column to the database. So it will look like ID, name, age, gender.

I solved this with onUpgrade(SQLiteDatabase db, int i, int i1), so if the version of DB is older as the current, it will run onUpgrade, when I add that new column via ALTER table.

So far so good. But what if the user has this new database already, but on Google drive has the old one and he restores that old database from cloud?

Will the onUpgrade run immediately when the db file is replaced with older version, or do I need to force to run onUpgrade after every restore from cloud?

Not sure when the onUpgrade is executed - only on the app start, or anytime when any DB operation is called and it sees the difference in versions?

CodePudding user response:

Will the onUpgrade run immediately when the db file is replaced with older version...

No, onUpgrade() is not invoked after the db is replaced.

do I need to force to run onUpgrade after every restore from cloud?

You should not call onUpgrade().

After the restore process is finished, you can call getWritableDatabase() or getReadableDatabase() which will open the database.
At this point the difference of the versions of the existing database and your code in your SQLiteOpenHelper class will invoke onUpgrade().

  • Related