Ale TheFe Ale TheFe - 1 month ago 9
Android Question

If I patch an Android App, my existing SQLite Database will be deleted and then recreated?

Obviously my question is about DIRECT UPDATE and not uninstall old version then install new version. There is nothing about it discussed on StackOverflow so I'm asking it to you all because I don't know what does Android do when performing updates.

If the answer is "yes, your db will be deleted and then recreated", then how can I NOT make this happen during an update?

Thank you

Answer

Your database will not be deleted by default. Although a very common example that people seem to follow for their SQLiteDatabaseHelper is this:

void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
   // DROP TABLES
}

Now when you create a new SQLiteDatabaseHelper and you pass in a versionNumber that's higher than the previous one, onUpgrade will be called and the tables will be dropped. This will not happen at all if the versions are the same. Likewise, the same will happen with onDowngrade if the version number is lower.

Comments