在Android上使用ORMLite重新打开连接

die*_*rre 2 sqlite testing android ormlite

我的情况是这样的.我有一个OrmLiteBaseActivity,其中有我的主菜单.当我按下按钮时,我进入"备份模式",我想从我的网站下载备份sqlite数据库.问题是,当我尝试上传文件时,我没有得到任何错误但数据库没有更新,直到我关闭并重新打开软件.我希望更新能够即时完成.

我试过我的OrmLiteBaseActivity是这样的:

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

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

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)
Run Code Online (Sandbox Code Playgroud)

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

Gra*_*ray 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;
}
Run Code Online (Sandbox Code Playgroud)

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

private ConnectionSource connectionSource = null;
@Override
public ConnectionSource getConnectionSource() {
    if (connectionSource == null) {
        connectionSource = super.getConnectionSource();
    }
    return connectionSource;
}
Run Code Online (Sandbox Code Playgroud)

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

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