在为数据库助手

时间:2017-02-16 05:56:04

标签: android android-sqlite refresh

我已将数据库备份和恢复合并到我的应用程序中。这一切都正常,但是当我恢复数据库时,由于使用单例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(注意扩展方法用于创建/修改表)

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中添加一个reopen方法,如下所示: -

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

然后在还原正常时根据代码在代码中调用它(根据注释)。但是,这导致以下结果: -

  

java.lang.IllegalStateException:尝试重新打开已经关闭的   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) 

2 个答案:

答案 0 :(得分:2)

诀窍很简单,不要关闭数据库只需重置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
                }

答案 1 :(得分:0)

对我有用的重新开放代码是:

public void reopen(Context context, String DbFilePath) {
        instance = new DBHelper(context, DbFilePath);
    }

与上一个答案的不同之处在于删除了&#34;静态&#34;从函数定义中,要求实例也是静态的。这阻止了实际重新打开数据库