Upgrading the SQLite Database of a Production Android App, is this Correct? Upgrading the SQLite Database of a Production Android App, is this Correct? sqlite sqlite

Upgrading the SQLite Database of a Production Android App, is this Correct?


When I need to update a database like this, I typically do it with a switch statement where cases fall through to one another, such as:

switch (oldVersion) {    case 1:        // update to version 2        // do _not_ break; -- fall through!    case 2:        // update to version 3        // again, do not break;    case 3:        // you're already up to date

The benefits to this is you do not end up repeating your update statements in multiple if-statements as you continue to change the database, and adding a database update requires only adding a new case statement, not updating multiple blocks of code.

There are sometimes exceptions to this, such as a column added in one version but then deleted in a future one, so you need to pay attention as you go.