Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onUpgrade() sqlite database in Android

Tags:

android

sqlite

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?

like image 844
GVillani82 Avatar asked Oct 31 '12 13:10

GVillani82


People also ask

What is the use of onUpgrade function in SQLiteOpenHelper?

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.

What is SQLite database in Android?

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.

What is SQLite open helper in Android?

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.


1 Answers

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);
    }
}
like image 130
Mr. T Avatar answered Sep 21 '22 00:09

Mr. T