How do I update my application through APK without losing my SQLite database?

5

I created an application, made registrations in the Sqlite database, but when I create new functions for the application how do I upgrade without uninstalling it?

    

asked by anonymous 12.09.2016 / 14:10

1 answer

1

When updating your APK in the playstore your database will NOT be deleted. In your databasehelper class you will have a onUpgrade method (SQLiteDatabase db, int oldVersion, int newVersion) where oldVersion is the previous version of your database on the user device and newVersion is the new version.

Example

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    switch (oldVersion){
        case 1;
            //TODO alteras campos da vesao 1 para versão 2
            break;
        case 2;
            //TODO alteras campos da vesao 2 para versão 3
            break;
        case 3;
            //TODO alteras campos da vesao 3 para versão 4
            break;
    }
}

If you need to know the new version of the bank to make the change use the variable newVersion

    
06.12.2016 / 22:13