Migrate data from one version to another - Sqlite Android


In the application I'm working on, I have to save the data from a specific table when the application is updated. This action must happen before the table is dropped in the onUpgrade method. I need to do this because when the application is updated I want the user to remain logged in to the system after the upgrade. User data is the only data I want to reuse. my onUpgrade method looks like this:

public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
try {

    TableUtils.dropTable(connectionSource, User.class, true);
    ... other tables

    // after we drop the old databases, we create the new ones
    onCreate(db, connectionSource);

} catch (SQLException e) {
    Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
    throw new RuntimeException(e);

How can I implement this persistence?

asked by anonymous 17.09.2015 / 16:18

2 answers


I do not know what kind of upgrade you want to do to the database, but this upgrade can be done with or without the need to delete and then re-create all the tables. To do this, in the onUpdate() method, use SQL commands of type ALTER TABLE and CREATE TABLE to make the changes.

If this is not feasible / practical or you just want to keep some data, read this data into memory, drop the tables, call the onCreate() method, and reset the saved data.     

17.09.2015 / 16:43

One option is for you not to save the user's database data, but rather to sharedPreferences , which do not work with upgrade.

18.09.2015 / 16:49