12

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.

Community
  • 1
  • 1
Maulik
  • 3,316
  • 20
  • 31
  • why don't you simply install both version successively ? I'm having trouble understanding what is missing here – njzk2 Oct 08 '13 at 13:43
  • @njzk2 Actually I have done changes in my code to upgrade db so I could not simply install my old version from eclipse directly and for new version I have tested by both ways simply install and with signed apk but no luck. – Maulik Oct 08 '13 at 14:01
  • you don't have the source of your previous release ?? – njzk2 Oct 08 '13 at 14:19
  • @njzk2 sorry,It was my mistake. I have also tested it by directly install on my device for both version older and newer of db. – Maulik Oct 08 '13 at 14:22
  • I don't understand your question, then – njzk2 Oct 08 '13 at 14:34
  • @njzk2 I have edited my question title, I want to test onUpgrade() method in which I am trying to create a new "table" which is not exist in old version app. And I want to test new "table" has been create in my next version app which I am going to upload it on play store. Hope you understood my question. – Maulik Oct 09 '13 at 07:35
  • then why simply installing the previous version and the new version is not sufficient for testing ? – njzk2 Oct 09 '13 at 07:56
  • @njzk2 onUpgrade method is not to be called whether I am using directly or installing apk files that's why I could not create my new table while testing. – Maulik Oct 09 '13 at 08:26
  • I would tend to indicate that your testing works, but your upgrade procedure fails the test. There is probably something wrong with your database code. may be the version value, the onUpgrade method that crashes ... – njzk2 Oct 09 '13 at 08:31
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38852/discussion-between-maulik-and-njzk2) – Maulik Oct 09 '13 at 08:33

4 Answers4

5

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.

Community
  • 1
  • 1
Leonidos
  • 10,482
  • 2
  • 28
  • 37
  • Thanks for the answer my question. can you please explain me in brief with giving me example to understand properly? – Maulik Oct 08 '13 at 14:03
  • 1
    db is just a file with data. version of the data is stored in the same file as well. If you want onUpgrade to be called you should open your current db file with sqlite editor and decrease db version. Next time you connect to db OnUpgrade will be called and you can check how it works. – Leonidos Oct 08 '13 at 14:12
  • You can also increase db version in DbHelper. And connect to your current db. OnUpgrade should be called. – Leonidos Oct 08 '13 at 14:17
  • Do you mean Schema Version or User version in the DB setting field? – Maulik Oct 08 '13 at 14:18
  • I mean increase version in SQLiteOpenHelper constructor. This number compared with db version number (stored in db file), and if it's larger onUpgrade will be called. – Leonidos Oct 08 '13 at 14:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38844/discussion-between-maulik-and-leonidos) – Maulik Oct 09 '13 at 07:15
  • upvote for the giving me quick response and nice explaination. I am trying the way as you told in your answer but still I could not test onUpgrade() to be called. I have changed version of my db (stored in db file) with version 1 which is less then given version in my question version 2(dbhelper version in constructor). but no luck. – Maulik Oct 09 '13 at 07:38
  • You can try second option (adb install -r) I mentioned. – Leonidos Oct 09 '13 at 12:46
  • Yes, Thanks for the link it helps me to test on emulator. I have tested as you guide me with "adb install -r" method but it does not give any success to call onUpgrade() to be call. – Maulik Oct 09 '13 at 13:19
3

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!

Community
  • 1
  • 1
Maulik
  • 3,316
  • 20
  • 31
1

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

Maulik Sheth
  • 584
  • 3
  • 10
  • sorry bro, I think you are not understanding my question properly. I want to test my newer version database before uploading apk on the play store as a new version of app. – Maulik Oct 08 '13 at 14:06
  • Yes thats what I'm trying to tell you, after making changes in yiur database tables and before uploading to play, make use of sqlite browser to check it your new changed database works or not – Maulik Sheth Oct 08 '13 at 14:59
  • I already have sqlite manager plugins for Firfox. And I have already tested with sqlite manager but my onupgrade() method is not to be called so my new table could not be create after upgrading my database. – Maulik Oct 09 '13 at 07:46
  • Thanks for the link to add plugins for eclipse. I have tested database with sqlite manager firefox plugins. I have solved this issue check my answer given below. Anyways thanks for the answer. – Maulik Oct 10 '13 at 05:50
1

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.

Peter Ambruzs
  • 7,763
  • 3
  • 30
  • 36