Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 588
Maulik Avatar asked Oct 08 '13 13:10

Maulik


People also ask

Which of the following method will you use for upgradation in SQLite?

onUpgrade. Called when the database needs to be upgraded. The implementation should use this method to drop tables, add tables, or do anything else it needs to upgrade to the new schema version.


2 Answers

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.

like image 173
Leonidos Avatar answered Sep 21 '22 14:09

Leonidos


After long search and testing my application, finally...

I got it working! In the createDataBase-class a line was missing:

I have just added below line in my code createDataBase()

this.getWritableDatabase();

We have to give permission to write database while we create database, this permission will be give permission to write the database while we upgrade the application from the play store.

Once opened successfully, the database is cached, so you can call this method every time you need to write to the database. (Make sure to call close() when you no longer need the database.) Errors such as bad permissions or a full disk may cause this method to fail, but future attempts may succeed if the problem is fixed.

Database upgrade may take a long time, you should not call this method from the application main thread, including from ContentProvider.onCreate().

public void createDataBase() throws IOException 
        { boolean dbExist = checkDataBase(); 
          if (!dbExist) {
           this.getReadableDatabase();
            try {
                // ---If not created then copy the database---
                copyDataBase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
          }
          else {
            /* This line: that has been solve my problem */
           this.getWritableDatabase();
          }
        }

public void openDataBase() throws SQLException {

      // --- Open the database---         String myPath = path + DATABASE_NAME;
      myDataBase = SQLiteDatabase.openDatabase(myPath, null,
              SQLiteDatabase.OPEN_READWRITE); // you also have to add open_readwrite condition here
              /*onUpgrade(myDataBase, 1, 2);*/ // no need to call upgrade     here it will be called automatically

  }

You have to give read write condition while open the database.

Furthermore onUpgrade() is called when a new object of the class DataBaseHelper is created but only if the database version numbers do not match. So this means that there is no need to call onUpgrade "on your own". If you publish an update of your db, just increment the version nr by one and apply changes in the onUpgrade()-method.

I got it from the given link:

onUpgrade database - oldVersion - newVersion

Hope this will helps anyone for future developer!

like image 23
Maulik Avatar answered Sep 20 '22 14:09

Maulik