Search code examples
androidsqliteormlitesqliteopenhelper

ORMLite data backup and restore on Schema Change


Im using ORMLite as the data persistence option in my android application. I want to ensure data backup and recovery on re-install. If i make some changes in the app logic and re-install the application the data is unchanged, but how can i handle the schema change, i the new version of app has some database schema changes how can i handle the import of user data into newer schema. Please guide me towards possible solutions i can avail.

Regards.


Solution

  • but how can i handle the schema change, i the new version of app has some database schema changes how can i handle the import of user data into newer schema

    If I'm understanding the question, it's not about import but it is about schema updating when you install a new version of the OS. ORMLite actually has a section about that:

    http://ormlite.com/docs/upgrade-schema

    To quote, you need to override the onUpgrade(...) method and do something like:

    abstract void onUpgrade(SQLiteDatabase database,
          ConnectionSource connectionSource, int oldVersion, int newVersion) {
        if (oldVersion < 2) {
          // we added the age column in version 2
          dao.executeRaw("ALTER TABLE `account` ADD COLUMN age INTEGER;");
        }
        if (oldVersion < 3) {
          // we added the weight column in version 3
          dao.executeRaw("ALTER TABLE `account` ADD COLUMN weight INTEGER;");
        }
    }
    

    If I'm not getting your question then please edit your post and I'll add more information.