Skip to content Skip to sidebar Skip to footer

Create New Table In Existing DB In Separate SQLiteOpenHelper Class

In my already created and deployed application, I've created a database MainDB, using a single class file which extended SQLiteOpenHelper, viz. public class BaseSQLiteOpenHelper ex

Solution 1:

First check the current database version for this database

private final static String DATABASE_NAME = "MainDB";
private static final int DATABASE_VERSION = 1;

public BaseSQLiteOpenHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

and increment the database version(DATABASE_VERSION), and add your new table query in on Upgrade and oncreate method like below.

@Override
public void onCreate(SQLiteDatabase db) {
      db.execSQL("old query no need to change");
      db.execSQL("Create your new table here");
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    if (oldVersion < 2) {
       db.execSQL("Create your new table here as well this for update the old DB");
    }
}

Done!!!


Post a Comment for "Create New Table In Existing DB In Separate SQLiteOpenHelper Class"