我有一个带有数据库的应用程序,在我的新应用程序版本中,必须更改数据库架构。
旧代码和新版本应该在代码中添加到哪里?新版本如何将数据库与旧版本进行比较并进行更改。
请帮我解决这个问题。
答案 0 :(得分:0)
Override onUpgrade method in SQLiteOpemHelper class.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.d("TAG", "ugrade called");
if (newVersion > oldVersion) {
db.execSQL("DROP TABLE IF EXISTS " + SchoolCode.CODE_LIST_TABLE);
// db.execSQL("DROP TABLE IF EXISTS " + ParentTable.PARENT_TABLE);
db.execSQL("DROP TABLE IF EXISTS "
+ PreferenceTable.PREFERENCE_LIST_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + ParentTable.PARENT_TABLE);
onCreate(db);
}
}
答案 1 :(得分:0)
//first DATABASE_VERSION add 1,bigger than your old version
private static final int DATABASE_VERSION = 2;
public ExpenseDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onUpgrade(SQLiteDatabase paramSQLiteDatabase, int oldVersion,
int newVersion) {
// TODO Auto-generated method stub
if (oldVersion == 1) {
// old version database ,you can upgrade data here
}
if (newVersion == 2) {
// new version database ,you can add data or table here
}
}