Java – how to test upgrading sqlite database before uploading new version of the app on play store in android

androidandroid-sqlitedatabasejavasqlite

I am getting issue no such table found "table_name" on my uploaded application after updating app version. I came out after testing I am getting this issue only after I upgrade the old version of app to new version of app

What I have Implemented

I have done code in the onUpgrade() method and also upgrade db version in constructor but I could not test it in the device.

step 1) Increase version code 1 to 2

public databasehelper(Context context) {

        super(context, DATABASE_NAME, null, 2);
        this.myContext = context;
        try {
            createDataBase();
            openDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

Step 2) added new table into onUpgrade() method

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        Log.e("onUpgrade db", ">>>>called");
        switch (oldVersion) {
        case 1:
            db.execSQL(DATABASE_CREATE_notification_counter);
            Log.e("db version", ">>>"+oldVersion);
            // we want both updates, so no break statement here...
        case 2:
            Log.e("db version", ">>>"+oldVersion);
                  // I removed onCreate() method from here because new version will already have the new table
//          onCreate(db);
        }

    }

Step 3) onCreate() method for the new version of database.

@Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE_notification_counter); 
        Log.d("oncreate db", ">>>>called");
    }

I have got the solution to upgrade database link1 link2 link3 link4

Why I could not test it

we have uploaded paid app on the play store so I have to test db upgrade method in emulator or device using two different apk means older version and newer version..
and I have tested in device first time using old version and then new version(db changes with added new table in db) but onUpgrade() method will not be called at this time.

Question:

1) How can I test db upgrade before updating new version on the
play store?

2) It will call only when you update the version from play store
only?

Please help me to solve this problem.

Answers will be appreciated. Thanks in advance.

Best Answer

Version of db stored inside db. Just create a db (db_old) with older version than in your DbHelper. Use DbHelper to connect to db_old and your onUpgrade method will be called.

You can use your existing db as db_old, just decrement it's version. To change version you can use any sqlite db editor.

If do not want to write test for DbHelper and want to simulate app upgrade, you can use adb install -r command. Read more here.