I created an Android application which, check at startup if there is a new version of application. If yes, the application download the new apk file and over-install new apk. My application use the sqlite db. But this db, from one version to other can change. I think I have to use the method:
onUpgrade()
but I don't know exactly how to use it.
When I start the application I use this code for crete database(if not exists):
DbHelper mDHelper = new DbHelper(context, DB_NAME, null, DB_VERSION)
What should I change if I want use onUpgrade()
method?
And when do I have to call it?
onUpgrade. Called when the database needs to be upgraded. The implementation should use this method to drop tables, add tables, or do anything else it needs to upgrade to the new schema version.
SQLite Database is an open-source database provided in Android which is used to store data inside the user's device in the form of a Text file. We can perform so many operations on this data such as adding new data, updating, reading, and deleting this data.
The android. database. sqlite. SQLiteOpenHelper class is used for database creation and version management. For performing any database operation, you have to provide the implementation of onCreate() and onUpgrade() methods of SQLiteOpenHelper class.
I found this very helpful https://thebhwgroup.com/blog/how-android-sqlite-onupgrade
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 2) {
db.execSQL(DATABASE_ALTER_TEAM_1);
}
if (oldVersion < 3) {
db.execSQL(DATABASE_ALTER_TEAM_2);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With