简体   繁体   中英

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.

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.

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!

how to test upgrading sqlite database without downloading new version app from play store in android

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

i downloaded sqlite browser plugin for eclipse from here

http://sourceforge.net/projects/sqlitebrowser/

and check this tutorial on how to use sqlite plugin

http://www.coderzheaven.com/2011/04/18/sqlitemanager-plugin-for-eclipse/

just copy the jar from 1st link and paste in your eclipse installation folder called plugin and restart eclipse

then start an emulator and navigate to your package from DDMS and you can view your sqlite database & check if the changes you made to tables exists or not

You can install apk from different source than Playstore (if it is enabled on your phone). The easiest way to send it to the phone by email or copy the apk via USB cable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM