gpt4 book ai didi

android - 自己的 SQLite 数据库和 onUpgrade

转载 作者:搜寻专家 更新时间:2023-10-30 22:03:40 25 4
gpt4 key购买 nike

我使用本教程来创建和填充我自己的适用于 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);
}

关于android - 自己的 SQLite 数据库和 onUpgrade,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6178582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com