Sqlite database onUpgrade() does not get called Sqlite database onUpgrade() does not get called sqlite sqlite

Sqlite database onUpgrade() does not get called


onUpgrade() is only called if a change in the database version number is detected. Increment the database version as follows:

private static final int DATABASE_VERSION = 2;


Change your onUpgrade to the following

@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {            if(newVersion > oldVersion){                                        myContext.deleteDatabase(DB_NAME);            }        }

And also change your createDataBase() like below,

public void createDataBase() throws IOException{            boolean dbExist = checkDataBase();            if(dbExist){                // By calling this method here onUpgrade will be called on a                // writable database, but only if the version number has been increased                this.getWritableDatabase();            }            dbExist = checkDataBase();            if(!dbExist){                //By calling this method an empty database will be created into the                     default system path                   //of the application so we will be able to overwrite that database with our database.                this.getReadableDatabase();                try {                    copyDataBase();                } catch (IOException e) {                    throw new Error("Error copying database");                }            }        }

At the end increase your database version and run it again. onUpgrade doesn't get called till you call getReadableDatabase() or similar functions.

Hope this helps


Try this (increase the DATABASE_VERSION) & the onUpdate() will be called:

private static final int DATABASE_VERSION = 2;