gpt4 book ai didi

android - 从 Assets 文件夹升级 Android SQLite 数据库

转载 作者:IT王子 更新时间:2023-10-29 06:31:06 26 4
gpt4 key购买 nike

我目前在我的 Assets 文件夹中有一个正在使用的数据库。我在升级它时遇到了问题。每当我设置 DATABASE_VERSION 编号时,它只会更新特定的时间。当它确实升级时,它会在启动时崩溃,我必须重新打开应用程序。

这是我的 createDatabase()

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()

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion > oldVersion){
Log.v("Database Upgrade", "Database version is higher than previous.");
myContext.deleteDatabase(DB_NAME);

}

我想删除我正在使用的旧数据库,让新数据库可用于应用程序布局。

这是将 createDataBase() 添加到 onUpgrade() 时我的 LogCat

03-06 15:17:15.348: V/DB Exists(2150): db exists
03-06 15:17:15.388: V/Database Upgrade(2150): Database version is higher than previous.
03-06 15:17:15.398: D/AndroidRuntime(2150): Shutting down VM
03-06 15:17:15.398: W/dalvikvm(2150): threadid=1: thread exiting with uncaught exception (group=0xb3f6d648)
03-06 15:17:15.418: E/AndroidRuntime(2150): FATAL EXCEPTION: main
03-06 15:17:15.418: E/AndroidRuntime(2150): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.productguide/com.example.productguide.PowersportsEquivalent}: java.lang.IllegalStateException: getDatabase called recursively
03-06 15:17:15.418: E/AndroidRuntime(2150): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
03-06 15:17:15.418: E/AndroidRuntime(2150): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-06 15:17:15.418: E/AndroidRuntime(2150): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-06 15:17:15.418: E/AndroidRuntime(2150): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-06 15:17:15.418: E/AndroidRuntime(2150): at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 15:17:15.418: E/AndroidRuntime(2150): at android.os.Looper.loop(Looper.java:137)
03-06 15:17:15.418: E/AndroidRuntime(2150): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-06 15:17:15.418: E/AndroidRuntime(2150): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 15:17:15.418: E/AndroidRuntime(2150): at java.lang.reflect.Method.invoke(Method.java:525)
03-06 15:17:15.418: E/AndroidRuntime(2150): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-06 15:17:15.418: E/AndroidRuntime(2150): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-06 15:17:15.418: E/AndroidRuntime(2150): at dalvik.system.NativeStart.main(Native Method)
03-06 15:17:15.418: E/AndroidRuntime(2150): Caused by: java.lang.IllegalStateException: getDatabase called recursively
03-06 15:17:15.418: E/AndroidRuntime(2150): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:204)
03-06 15:17:15.418: E/AndroidRuntime(2150): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
03-06 15:17:15.418: E/AndroidRuntime(2150): at com.example.productguide.DataBaseHelper.createDataBase(DataBaseHelper.java:143)
03-06 15:17:15.418: E/AndroidRuntime(2150): at com.example.productguide.DataBaseHelper.onUpgrade(DataBaseHelper.java:248)
03-06 15:17:15.418: E/AndroidRuntime(2150): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:257)
03-06 15:17:15.418: E/AndroidRuntime(2150): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
03-06 15:17:15.418: E/AndroidRuntime(2150): at com.example.productguide.DataBaseHelper.createDataBase(DataBaseHelper.java:136)
03-06 15:17:15.418: E/AndroidRuntime(2150): at com.example.productguide.PowersportsEquivalent.onCreate(PowersportsEquivalent.java:57)
03-06 15:17:15.418: E/AndroidRuntime(2150): at android.app.Activity.performCreate(Activity.java:5133)
03-06 15:17:15.418: E/AndroidRuntime(2150): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-06 15:17:15.418: E/AndroidRuntime(2150): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
03-06 15:17:15.418: E/AndroidRuntime(2150): ... 11 more

最佳答案

通过 Google 浏览时,我找到了问题的答案。

通过将下面的代码添加到我的 copyDataBase()

SQLiteDatabase checkdb = null;

try{
String myPath = DB_PATH + DB_NAME;
checkdb = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

// Once the DB has been copied, set the new version
checkdb.setVersion(DATABASE_VERSION);
}
catch(SQLiteException e)
{
//database does’t exist yet.
}

并且在 onUpgrade()

中包含以下代码
if (oldVersion < newVersion){
Log.v("Test", "Within onUpgrade. Old is: " + oldVersion + " New is: " + newVersion);
myContext.deleteDatabase(DB_NAME);
try {
copyDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

我的数据库现在升级到数据库并在 DATABASE_VERSION 整数每次更改时删除以前的数据库。

关于android - 从 Assets 文件夹升级 Android SQLite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22183912/

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