I am a bit confused with the android SQLite database handling. I went through tutorials but I didn’t get the exact point.
We can have a database class that extends SQLiteOpenHelper, and can override onCreate() method and create a database.
Upgrading database part is a bit confusing. In the following method, how to handle the verions
onUpdate(SQLiteDatabase db,int old Version,int newVerison)
Does it mean that when the first time we create database the version is 1. Then, once modified the version become 2.
Then if we want to modify again
old Version = 2, newVerison = 3
[onUpdate(SQLiteDatabase db,int old Version,int newVerison) ]
This method will be execute when we pass the constructor version as in the following code (as 2)
public DatabaseHelper(Context context) {
super(context, dbName, null,2);
}
I need to know whether when we need to call onUpgrade() method should we pass version as 2 always or we have to increase one every time for the previous version.
Increment your database version every time the schema changes.
You never need to call
onUpgradedirectly. Android will call it when required when you open the database by comparing the version of the database with the version that your code is specifying as the current version.You just need to handle the upgrade process in
onUpgrade– it might go something like this:Lets say your newVersion is 4 and oldVersion is 1 – first iteration will increment curVer to 2 and will run the V2 to V3 upgrade code. Second iteration increments curVer to 3 and runs the V2 to V3 upgrade code, final iteration increments curVer to 4 and runs the V3 to V4 upgrade code.
This works for all values of oldVersion that are less than newVersion. and will sequentially upgrade through the intermediate versions if your users are skipping upgrades of the app.