Search code examples
androidsqliteonupdate

onUpgrade database Android version number not increasing


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?


Solution

  • 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.