7

I have a database that I want to add a column to, I changed the static database version but when I run the program on my device the new column is still not there so I am guessing my onUpdate is not getting called and I am not sure why.

this is how I create the database

     private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            createTables(db);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
                              int newVersion) 
        {
            Log.w("CalendarDB", "Upgrading database from version " + oldVersion 
                  + " to "
                  + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS Events_Table");
            onCreate(db);
        }

        private void createTables(SQLiteDatabase db){
            db.execSQL("CREATE TABLE " + EVENTS_TABLE + "(" + ID + " integer primary key autoincrement, " +
                    EVENT + " TEXT, " + LOCATION + " TEXT, " + DESCRIPTION + " TEXT, " + DATE + " TEXT, " + START + " LONG, " + END + " TEXT, " + REAL_START_TIME + " TEXT," 
                    + REAL_END_TIME + " TEXT," + COLOR + " TEXT, " + BLINK + " TEXT, " + VIBRATE + " TEXT, " + RING_TONE_PATH + " TEXT, " + ICON + " TEXT, " + REMINDERS +
                    " TEXT, " + START_WITH_REMINDER + " TEXT, " + CALENDAR_ID + " TEXT, " + EVENT_ID + " TEXT);");
        }
    }

is there something else I have to do to get the onUpdate to get called? I tried following this answer but still no result

Community
  • 1
  • 1
tyczj
  • 71,600
  • 54
  • 194
  • 296

2 Answers2

12

In order to upgrade the Database in Android you should increment the DATABASE_VERSION by one, in order for the SQLOpenHelper to know that it must called the onUpgrade method.

Rembember

This only work when you call getWritableDatabase() otherwise it won't upgrade. And if you change the version and call getReadableDatabase it will show an exception

throw new SQLiteException("Can't upgrade read-only database from version " +
                        db.getVersion() + " to " + mNewVersion + ": " + path);

But Why? You see when you call getWritableDatabase the code check wether the version is the same:

if (version != mNewVersion) {
                db.beginTransaction();
                try {
                    if (version == 0) {
                        onCreate(db);
                    } else {
                        if (version > mNewVersion) {
                            onDowngrade(db, version, mNewVersion);
                        } else {
                            onUpgrade(db, version, mNewVersion);
                        }
                    }
                    db.setVersion(mNewVersion);
                    db.setTransactionSuccessful();
                } finally {
                    db.endTransaction();
}
}

Update

Well turns out that it should work with getReadableDatabase() since in the code you always are getting a WrittableDatabase too. So it should work with both method, here is the documentation for getReadableDatabase():

Create and/or open a database. This will be the same object returned by getWritableDatabase() unless some problem, such as a full disk, requires the database to be opened read-only. In that case, a read-only database object will be returned. If the problem is fixed, a future call to getWritableDatabase() may succeed, in which case the read-only database object will be closed and the read/write object will be returned in the future.

Necronet
  • 6,704
  • 9
  • 49
  • 89
  • so where do I call getWritableDatabase then? should I call it in my constructor? – tyczj Dec 24 '11 at 23:17
  • wherever you are going to use it... I in general use it in my ContentProvider but if you don't have one, it should goes fluently in your code, any where you are going to insert or update a getWritableDatabase has to be called, it's not necesary to force it's use. – Necronet Dec 24 '11 at 23:51
  • @tyczj ~"where do I call getWritableDatabase then?" I am assuming you are using the SQLiteOpenHelper class for doing CRUD operations? In that case you would call getWritableDatabase() when you obtain an instance of SQLiteOpenHelper in your code. – IgorGanapolsky Dec 13 '13 at 16:09
1

I suggest you increment DATABASE_VERSION by one, that will lead the application to call onUpgrade()

prometheuspk
  • 3,754
  • 11
  • 43
  • 58