[英]Own SQLite Database and onUpgrade
我使用本教程為 android 創建和填充我自己的 SQLite 數據庫。 http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications
但是,使用這些確切的方法意味着數據庫不會使用以下代碼升級
private static final int DB_Version = 2;
public DbConnector(Context context){
super(context, DB_Name, null, DB_Version);
this.context = context;
}
事實上,onUpgrade() 方法從未被調用:(
感謝任何幫助
這是感謝 Joe Masilotti 在http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/comment-page-2/上的答案
private static final int DATABASE_VERSION = 1;
將構造函數更改為:
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
this.myContext = context;
}
將createDataBase
更改為(感謝@kondortek):
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
Log.v(“DB Exists”, “db exists”);
// By calling this method here onUpgrade will be called on a
// writeable database, but only if the version number has been
// bumped
this.getWritableDatabase();
}
dbExist = checkDataBase();
if (!dbExist) {
// By calling this method and empty database will be created into
// the default system path of your application so we are gonna be
// able to overwrite that database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error(“Error copying database”);
}
}
}
將onUpgrade
更改為:
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion > oldVersion)
Log.v(“Database Upgrade”, “Database version higher than old.”);
myContext.deleteDatabase(DB_NAME);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.