2

我的情况是这样的。我有一个 OrmLiteBaseActivity,那里有我的主菜单。当我按下一个按钮时,我进入“备份模式”,我想从我的网站下载一个备份 sqlite db。问题是,当我尝试上传文件时,我没有收到任何错误,但数据库不会更新,直到我关闭并重新打开软件。我希望更新是即时的。

我在我的 OrmLiteBaseActivity 上试过这样的东西:

case BACKUP_ID:
    getHelper().close();
    Intent i = new Intent(this, Backup.class);
    this.startActivity(i);
    return true;

我去备份活动,更新文件,然后我想回去,但我得到了这个错误:

11-15 19:27:45.359: ERROR/DatabaseHelper(229): Getting connectionSource called after closed
11-15 19:27:45.359: ERROR/DatabaseHelper(229): java.lang.IllegalStateException
11-15 19:27:45.359: ERROR/DatabaseHelper(229):     at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.getConnectionSource(OrmLiteSqliteOpenHelper.java:78)
11-15 19:27:45.359: ERROR/DatabaseHelper(229):     at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.getDao(OrmLiteSqliteOpenHelper.java:171)
11-15 19:27:45.359: ERROR/DatabaseHelper(229):     at info.dierrelabs.h4m.ormliteinterface.DatabaseHelper.getPlayerDao(DatabaseHelper.java:159)
11-15 19:27:45.359: ERROR/DatabaseHelper(229):     at info.dierrelabs.h4m.team.TeamList.onCreate(TeamList.java:20)
11-15 19:27:45.359: ERROR/DatabaseHelper(229):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-15 19:27:45.359: ERROR/DatabaseHelper(229):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
11-15 19:27:45.359: ERROR/DatabaseHelper(229):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
11-15 19:27:45.359: ERROR/DatabaseHelper(229):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
11-15 19:27:45.359: ERROR/DatabaseHelper(229):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
11-15 19:27:45.359: ERROR/DatabaseHelper(229):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 19:27:45.359: ERROR/DatabaseHelper(229):     at android.os.Looper.loop(Looper.java:123)
11-15 19:27:45.359: ERROR/DatabaseHelper(229):     at android.app.ActivityThread.main(ActivityThread.java:4363)
11-15 19:27:45.359: ERROR/DatabaseHelper(229):     at java.lang.reflect.Method.invokeNative(Native Method)
11-15 19:27:45.359: ERROR/DatabaseHelper(229):     at java.lang.reflect.Method.invoke(Method.java:521)
11-15 19:27:45.359: ERROR/DatabaseHelper(229):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-15 19:27:45.359: ERROR/DatabaseHelper(229):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-15 19:27:45.359: ERROR/DatabaseHelper(229):     at dalvik.system.NativeStart.main(Native Method)

有什么我想念的吗?我假设每次打开新的 OrmLiteBaseActivity 时使用 OrmLiteBaseActivity 都会重新打开数据库。难道我做错了什么?

4

2 回答 2

5

你没有做错什么,但你正在做一些非典型的事情。

在中,它会记录一个错误,因为有人在被调用OrmLiteSqliteOpenHelper.getConnectionSource()后试图获取连接源:close()

public ConnectionSource getConnectionSource() {
    if (!isOpen) {
        // we don't throw this exception, but log it for debugging purposes
        logger.error(new IllegalStateException(), 
                    "Getting connectionSource called after closed");
    }
    return connectionSource;
}

您可以在帮助程序中覆盖此方法以抛出消息:

private ConnectionSource connectionSource = null;
@Override
public ConnectionSource getConnectionSource() {
    if (connectionSource == null) {
        connectionSource = super.getConnectionSource();
    }
    return connectionSource;
}

这将修复错误日志消息,但问题是副本和新数据库是否有效。您可能想看看这两个数据库代码示例。它维护自己的数据库计数器等您可能需要做的事情:

http://ormlite.com/docs/android-hello-two-dbs

于 2011-11-16T13:28:13.033 回答
2

所以我解决了。当用户按下备份按钮时,会发生以下情况:

case BACKUP_ID:
    getHelper().close();
    Intent i = new Intent(this, Backup.class);
    this.startActivity(i);
    return true;

那么我需要重新打开这个连接。您实际上可以这样做(在我交换 sqlite 文件之后):

            DatabaseHelper db = new DatabaseHelper(Backup.this);
            db.getWritableDatabase();

该类Backup只是扩展Activity

于 2011-11-29T18:14:35.010 回答