Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does OnUpgrade get called and how does it get the oldVersion parameter?

I am working on an update for my current app. My app use SQLite DB and so far I am at version 1. I will need to alter/introduce new tables in my second version. I did my research and I found that to do that the best way is to have switch(case) statements in which I do the updates to db in a fall-through manner.

My question is, how does android know what is the older version in the onUpgrade()?. In fact, when does it get called?.

So for the users who are downloading my updated app for first time, I assume the onUpgrade() will not be called! How would android know when not to call onUpgrade()?

Please clarify this for me. I want to submit my first update and I don't want to lose my hard-earned users :)

like image 663
Snake Avatar asked Jan 17 '13 02:01

Snake


People also ask

What is onUpgrade in SQLite?

Android SQLite onUpgrade() method In this class, the onUpgrade() method is responsible for upgrading the database when you make changes to the schema. It is called when the database file already exists, but its version is lower than the one specified in the current version of the app.

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. The SQLite ALTER TABLE documentation can be found here.

What is onCreate in SQLite?

onCreate() method is creating the tables you've defined and executing any other code you've written. However, this method will only be called if the SQLite file is missing in your app's data directory ( /data/data/your. apps.


2 Answers

When implementing the SQLiteOpenHelper, you pass a version parameter to the superclass constructor. This should be a static value you can change in future versions of your application (usually you'd want this to be a static final attribute of your SQLiteOpenHelper implementation, alongside the database name you pass to the superclass constructor).

Then, when you want to update the database, you increment the version parameter that will be used in your SQLiteOpenHelper implementation, and perform the SQL modifications you intend to do, programmatically, inside the onUpgrade() method.

Say your DB started with table A in version 1, then you added table B in version 2 and table C in version 3, your onUpgrade method would look like:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (oldVersion < 2) { // do stuff for migrating to version 2
        db.execSQL( ...create table B... );
    }
    if (oldVersion < 3) { // do stuff for migrating to version 3
        db.execSQL( ...create table C... );
    }
}

When the superclass constructor runs, it compares the version of the stored SQLite .db file against the version you passed as a parameter. If they differ, onUpgrade() gets called.

I should check the implementation, but I assume onUpgrade() is also called after onCreate() if the database needs to be created, and there's a way to ensure all of the upgrades are executed (for example, by forcing all version numbers to be positive integers and initializing a newly created database with version 0).

like image 95
Martín Valdés de León Avatar answered Oct 11 '22 23:10

Martín Valdés de León


onUpgrade() is NOT called from the constructor. The first time getWritableDatabase() is called, the database will be opened onCreate(), onUpgrade() and/or onOpen() will be called. That's why the constructor is on the main thread, but it's a better performance practice to call getWritableDatabase() from a worker thread.

like image 31
Barry Li Avatar answered Oct 11 '22 23:10

Barry Li