1

I am trying to updgrade the sqlite version number from 1 to 2 but onupgrade method is not getting called.

do i have to delete application in the device and then install the application to test it ?

Only method which get called is DatabaseHelper and onCreate No other method get called.

In DatabaseHelper.java file

    private static final int DBVersion = 1; // I had change this value to 2.but it is not working.
    public DatabaseHelper(Context context, CursorFactory cf) {
        super(context, DBName, cf, DBVersion);
    }

 @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(TABLE_CREATE_Table);
}
@Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
} 
@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
//This is not called.If it called i will add the changed here
}

another class for dataprovider.java

@Override
    public boolean onCreate() {
        dbHelper = new DatabaseHelper(getContext(), null);
        return true;
    }
ios developer
  • 3,363
  • 3
  • 51
  • 111
  • 1
    Possible duplicate of [Why is onUpgrade() not being invoked on Android sqlite database?](https://stackoverflow.com/questions/7647566/why-is-onupgrade-not-being-invoked-on-android-sqlite-database) – bmcglynn Jun 06 '19 at 20:43
  • 2
    How do you know that onUpgrade was not called? Did you add a breakpoint and debug? Did you add any code the check? Did you try to do anything e.g. insert a row? onUpgrade will only be called when you attempt to do something. it **will not** be called by simply instantiating the database helper. – MikeT Jun 06 '19 at 20:46
  • @MikeT had added breakpoint over there to check if that is called or not. Yes insert / update method are getting called. But do i have to check the condition in getWritableDatabase() that if the version is updated then explicitly call onupgrade? – ios developer Jun 06 '19 at 20:48
  • @MikeT Thank you mike. I got the answer. Onupgrade get called after i try to insert the data. you were right. thank you for helping – ios developer Jun 06 '19 at 20:52

1 Answers1

2

using :-

@Override
public boolean onCreate() {
    dbHelper = new DatabaseHelper(getContext(), null);
    SqliteDatabase db = dbHelper.getWritableDatabase(); //<<<<<<<<<<< 
    return true;
}

attempts to open the database and thus onCreate/onUpgrade would be called. - onCreate only if the database does not exist. - onUpgrade only if the database exists AND the specified version number is greater than the database version stored in the database.

That is when instantiating the Database Helper (subclass of SQLiteOpenHelper) no attempt is made to open the database.

The attempt to open the database is only made when the SQLiteDatabase's getWritableDatabase (or getReadableDatabase) are called. Both attempt to open the database. Noting that getWritableDatabase or getReadableDatabase may well be called implicitly.

  • Note the above does not include directly using the SQliteDatabase's OPEN methods.

Alternative Fix

I personally tend to force the open when constructing the database helper by using :-

public DatabaseHelper(Context context, CursorFactory cf) {
    super(context, DBName, cf, DBVersion);
    this.getWritableDatabase();
}
  • I tend to save the returned SQLiteDatabase into a class variable and then use that rather than using this.getWritableDatabase() in the underlying methods.
MikeT
  • 51,415
  • 16
  • 49
  • 68