gpt4 book ai didi

android - 当使用单例作为数据库助手时,如何在恢复后刷新/重新打开 SQLite 数据库

转载 作者:行者123 更新时间:2023-11-29 19:21:50 25 4
gpt4 key购买 nike

我在我的应用程序中加入了数据库备份和恢复。这一切都很好,除了当我恢复数据库时,由于使用单例 DBHelper 以及仅在应用程序关闭时关闭数据库,数据库的内存副本在恢复后继续使用。

关闭应用程序并重新启动使用新数据库。但是,而不是告诉用户这样做。我想无缝访问恢复后的数据库。

这是检测并报告成功恢复的代码:-

                if(copytaken && origdeleted && restoredone) {
errlist.add("Database successfully restored." +
"\n\nYou should close the ShopWise App and then restart it.");
resulttitle = "Restore was successful.";
//DBHelper.reopen(context); <== implemented as below
}
..... Displays dialog with text from above

这是 DBHelper(注意 expand 方法用于创建/修改表)

class DBHelper extends SQLiteOpenHelper {

private static final String DBNAME = DBConstants.DATABASE_NAME;

/**
* Consrtuctor
*
* @param context activity context
* @param name database name
* @param factory cursorfactory
* @param version database version
*/
DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}

/**
* Instantiates a new Db helper.
*
* @param context the context
*/
DBHelper(Context context) {
super(context, DBConstants.DATABASE_NAME, null, 1);
}

private static DBHelper instance;

/**
* Gets helper.
*
* @param context the context
* @return the helper
*/
static synchronized DBHelper getHelper(Context context) {
if(instance == null) {
instance = new DBHelper(context);
}
return instance;
}

@Override
public void onCreate(SQLiteDatabase db) {
expand(db, false);
}
.......

认为您应该关闭数据库并重新打开它。我尝试在 DBHelper 中添加重新打开方法,如下所示:-

public static void reopen(Context context) {
instance.close();
instance = null;
instance = new DBHelper(context);
}

然后在恢复正常时从代码中调用它(根据评论)。但是,这会导致以下结果:-

java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase:

02-16 16:41:20.938 2683-3050/mjt.shopwise E/SQLiteLog: (28) file unlinked while open: /data/data/mjt.shopwise/databases/ShopWise
02-16 16:41:25.171 2683-2683/mjt.shopwise D/AndroidRuntime: Shutting down VM
02-16 16:41:25.171 2683-2683/mjt.shopwise E/AndroidRuntime: FATAL EXCEPTION: main
Process: mjt.shopwise, PID: 2683
java.lang.RuntimeException: Unable to resume activity {mjt.shopwise/mjt.shopwise.MainActivity}: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/mjt.shopwise/databases/ShopWise
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2986)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/mjt.shopwise/databases/ShopWise
at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1312)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
at mjt.shopwise.DBCommonMethods.getTableRows(DBCommonMethods.java:106)
at mjt.shopwise.DBCommonMethods.getTableRows(DBCommonMethods.java:59)
at mjt.shopwise.DBCommonMethods.getTableRowCount(DBCommonMethods.java:29)
at mjt.shopwise.DBCommonMethods.getTableRowCount(DBCommonMethods.java:43)
at mjt.shopwise.DBShopMethods.getShopCount(DBShopMethods.java:44)
at mjt.shopwise.MainActivity.getDBCounts(MainActivity.java:207)
at mjt.shopwise.MainActivity.onResume(MainActivity.java:163)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1257)
at android.app.Activity.performResume(Activity.java:6076)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2975)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1347) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5254) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

最佳答案

技巧很简单,不要关闭数据库只需重置DBHelper即可。

所以重新打开方法可以是:-

public static void reopen(Context context) {
instance = new DBHelper(context);
}

当然,您也可以取消告诉用户关闭并重新启动应用程序的文本。

因此,检测并报告成功恢复的代码可能是:-

if(copytaken && origdeleted && restoredone) {
errlist.add("Database successfully restored.");
resulttitle = "Restore was successful.";
DBHelper.reopen(context); <== implemented as below
}

关于android - 当使用单例作为数据库助手时,如何在恢复后刷新/重新打开 SQLite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42266031/

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