public class MyHelper extends SQLiteOpenHelper {
public static final String CREATE_BOOK="create table book("
+"id integer primary key autoincrement,"
+"author text,"
+"price real,"
+"pages integer,"
+"name text)";
private Context mContext;
public MyDataBaseHelper(Context context,String name,SQLiteDatabase.CursorFactory factory,int version)
{
super(context,name,factory,version);
mcontext=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);
}
@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:
}
}
}
升級數(shù)據(jù)庫方式一:
onUpgrade()方法是用于對數(shù)據(jù)庫進(jìn)行升級的彭雾。
假如現(xiàn)在要在數(shù)據(jù)庫中添加一個STUDENT的表碟刺,如何在上述程序上修改呢?
直接在onCreate()方法中添加創(chuàng)建語句是不行的薯酝,因?yàn)閿?shù)據(jù)庫已經(jīng)存在半沽,OnCreate()方法不會被運(yùn)行了。那最基礎(chǔ)的辦法是:
傳入一個高一點(diǎn)的版本號吴菠,
在onUpgrade運(yùn)行刪除數(shù)據(jù)庫的操作者填,在點(diǎn)擊按鈕就可以運(yùn)行OnCreate()方法了。
傳入一個高一點(diǎn)的版本號做葵,就可以讓onUpgrade()方法執(zhí)行
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_STUDENT="create table Student("
+"id integer primary key autoincrement,"
+"name text,"
+"age integer)";
private Context mContext;
public MyDataBaseHelper(Context context,String name,SQLiteDatabase.CursorFactory factory,int version)
{
super(context,name,factory,version);
mContext=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);
db.execSQL(CREATE_STUDENT);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists Book");
db.execSQL("drop table if exists Student");
onCreate(db);
}
}
}
優(yōu)點(diǎn):寫法簡單占哟,缺點(diǎn)會刪除歷史數(shù)據(jù),造成隱患
升級數(shù)據(jù)庫方式二:
增加數(shù)據(jù)庫版本號(為每一個版本號賦予它各自的更改的內(nèi)容)酿矢,在onUpgrade()方法中對當(dāng)前數(shù)據(jù)庫的版本號進(jìn)行判斷榨乎,再執(zhí)行相應(yīng)的改變就可以了。
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_STUDENT="create table Student("
+"id integer primary key autoincrement,"
+"name text,"
+"age integer)";
public MyDataBaseHelper(Context context,String name,SQLiteDatabase.CursorFactory factory,int version)
{
super(context,name,factory,version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch (oldVersion) {
case 1:
db.execSQL(CREATE_STUDENT);
case 2:
db.execSQL("alter table Book add column category_id integer");
default:
}
}
}
SQLite數(shù)據(jù)庫只允許增加表字段(alter table my_table add field_name field_type;)瘫筐,不允許修改和刪除表字段蜜暑。