1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 public class MyDatabaseHelper extends SQLiteOpenHelper { public static final String CREATE_BOOK = "create table Book (" + "id integer primary key autoincrement, " + "author text, " + "price real, " + "pages integer, " + "name text, " + "category_id integer)" ; public static final String CREATE_CATEGORY = "create table Category (" + "id integer primary key autoincrement, " + "category_name text, " + "category_code integer)" ; public MyDatabaseHelper (Context context, String name,CursorFactory factory, int version) { super (context, name, factory, version); } @Override public void onCreate (SQLiteDatabase db) { db.execSQL(CREATE_BOOK); db.execSQL(CREATE_CATEGORY); } @Override public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) { switch (oldVersion) { case 1 : db.execSQL(CREATE_CATEGORY); case 2 : db.execSQL("alter table Book add column category_id integer" ); default : } } }
notice there is no key word ‘break’ inside the switch block, therefore, no matter which version that user installed, the db update will be ran properly, and keep the database up to date.