3

I am trying to update my database in my Android application. When I update the version number, onUpgrade gets called, but the version number doesn't increase, so every time I access the database, onUpgrade gets called. Here is my code:

private final static int DB_VERSION = 8;

        public DataBaseHelper(Context context) {

        super(context, DB_NAME, null, DB_VERSION);
        this.myContext = context;
    }   
        @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d(TAG, "in onCreate");
        try {
            copyDataBase();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.d(TAG, "in onUpgrade. Old is: " + oldVersion + " New is: " + newVersion);
        myContext.deleteDatabase(DB_NAME);
        Log.d(TAG, "the version is " + db.getVersion());
        db.setVersion(newVersion);
        Log.d(TAG, "the version is " + db.getVersion());
        onCreate(db);           
    }

Anyone know why this is happening?

coder
  • 10,460
  • 17
  • 72
  • 125
  • maybe thil link may be helpful http://stackoverflow.com/questions/7173896/onupgrade-database-oldversion-newversion – Georgy Gobozov Sep 23 '11 at 23:09
  • I had already looked at this link...I still can't figure it out. Even though the database version number is static, the version number doesn't get upgraded in onUpgrade(). OnUpgrade is called, but the version number doesn't get updated afterward... – coder Sep 26 '11 at 12:54
  • What are the values of oldVersion and newVersion when your onUpgrade is called? – Graham Borland Sep 26 '11 at 12:57

2 Answers2

2

Don't try to manually set the version. (This is all taken care of.)

Don't try to delete the database. You're in the middle of upgrading it, and it shouldn't really surprise you that manually setting the version of a database you've just deleted doesn't work very well.

All you should do in onUpgrade is make the changes needed to the structure of the tables. If you want to do this by deleting the current tables (NOT the database) and then re-creating them, then that's fine.

If you need to preserve the data in your tables, have a look at this question for suggestions on how to do it: Upgrade SQLite database from one version to another?

Community
  • 1
  • 1
Graham Borland
  • 60,055
  • 21
  • 138
  • 179
  • According to the api, context.deleteDatabase() deletes the entire database. I think I do want to delete the entire thing since the structure and all of the 2000+ entries of my database need to change? All the tables need to change as well... – coder Sep 26 '11 at 13:14
  • No, trying to delete the database will seriously screw things up here. Just delete the tables, and re-create them. – Graham Borland Sep 26 '11 at 13:16
  • How can I rewrite just the tables from the assets folder and not the entire database? – coder Sep 26 '11 at 13:40
  • sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); – Kurovsky Feb 18 '15 at 09:31
0

I ended up adding another database to the assets folder. I know its not the best solution, but it works. When the app is upgraded this time, it just writes the new database - it has a new name - instead of the old one. I check if the old one exists and if it does, I delete it.

coder
  • 10,460
  • 17
  • 72
  • 125