0

I develop an application that already live on GooglePlay, Now I need to modify some parts that need more column in the database. But I got problem, The problem is when I post the new APK with more column to playstore, I can't found those new column in the database, but when I uninstall it first, then I install the new APK, the problem is gone,

How to fix this bug?

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
  • You can use SQLiteOpenHelper's onUpgrade method. In that you have to alter table for or drop table, then create new table for those who are using previous version of app. Also you have to update the database version. – Manish Jain Nov 13 '17 at 04:26

1 Answers1

1

you have to use onUpgrade() method to reflect changes in local and live application.

onUpgrade() is only called when the database file exists but the stored version number is lower than requested in constructor. The onUpgrade() should update the table schema to the requested version.

When changing the table schema in code (onCreate()), you should make sure the database is updated. Two main approaches:

  1. Delete the old database file so that onCreate() is run again. This is often preferred at development time where you have control over the installed versions and data loss is not an issue. Some ways to to delete the database file:

    • Uninstall the application. Use the application manager or adb uninstall your.package.name from shell.

    • Clear application data. Use the application manager.

  2. Increment the database version so that onUpgrade() is invoked. This is slightly more complicated as more code is needed.

    • For development time schema upgrades where data loss is not an issue, you can just use execSQL("DROP TABLE IF EXISTS <tablename>") in to remove your existing tables and call onCreate() to recreate the database.

    • For released versions, you should implement data migration in onUpgrade() so your users don't lose their data.


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    switch(oldVersion){
        case 1:
            db.execSQL("ALTER TABLE " + DATABASE_TABLE + " ADD COLUMN " + NEW_COLUMN_NAME + TYPE);
    }
} 

Check SqlLiteOpenHelper and this explanation

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142