安卓& OrmLite:OnUpgrade 失败

发布于 2024-10-04 07:40:56 字数 1691 浏览 3 评论 0原文

我在 Android 上使用 OrmLite 时遇到一个小问题。

当我增加数据库版本时,onUpgrade 方法将按我的 OrmLite Helper 中的预期进行调用。升级后,调用 onCreate 方法,并出现此异常:

11-24 10:09:45.720: ERROR/AndroidConnectionSource(390): connection saved
    com.j256.ormlite.android.AndroidDatabaseConnection@44f0f478 is not the one
    being cleared com.j256.ormlite.android.AndroidDatabaseConnection@44f5d310

我不知道为什么清除的连接与保存的连接不同。

我还将我的数据库函数(插入...)放入 OrmLite Helper 类中。也许这可能是一个问题?!?

我的助手类中的一个片段:

public class OrmLiteDBProvider extends OrmLiteSqliteOpenHelper
    implements IEntityProvider, IDBProvider {

//snip
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
    try {
        Log.i(OrmLiteDBProvider.class.getName(), "Creating database and tables");
        TableUtils.createTable(connectionSource, OrgManaged.class);
    } catch (SQLException e) {
        Log.e(OrmLiteDBProvider.class.getName(),
            "Can't create database and tables", e);
        throw new RuntimeException(e);
    }
}
@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
   int oldVersion, int newVersion) {
    try {
        Log.i(OrmLiteDBProvider.class.getName(),
            "Database version changed. Dropping database.");
        TableUtils.dropTable(connectionSource, OrgManaged.class, true);
        // after we drop the old databases, we create the new ones
        onCreate(db);
    } catch (SQLException e) {
        Log.e(OrmLiteDBProvider.class.getName(), "Can't drop databases", e);
        throw new RuntimeException(e);
    }
}

我认为这是我缺少的一些简单的东西。

预先感谢您的努力。

I have a small problem with OrmLite on Android.

When I increment the database version, the onUpgrade method is called as expected in my OrmLite Helper. After the upgrade, the onCreate method is called and I get this exception:

11-24 10:09:45.720: ERROR/AndroidConnectionSource(390): connection saved
    com.j256.ormlite.android.AndroidDatabaseConnection@44f0f478 is not the one
    being cleared com.j256.ormlite.android.AndroidDatabaseConnection@44f5d310

I have no clue why the cleared connection is not the same as the saved one.

I've put also my database functions (insert...) into the OrmLite Helper class. Maybe this could be a problem?!?

A snippet from my helper class:

public class OrmLiteDBProvider extends OrmLiteSqliteOpenHelper
    implements IEntityProvider, IDBProvider {

//snip
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
    try {
        Log.i(OrmLiteDBProvider.class.getName(), "Creating database and tables");
        TableUtils.createTable(connectionSource, OrgManaged.class);
    } catch (SQLException e) {
        Log.e(OrmLiteDBProvider.class.getName(),
            "Can't create database and tables", e);
        throw new RuntimeException(e);
    }
}
@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
   int oldVersion, int newVersion) {
    try {
        Log.i(OrmLiteDBProvider.class.getName(),
            "Database version changed. Dropping database.");
        TableUtils.dropTable(connectionSource, OrgManaged.class, true);
        // after we drop the old databases, we create the new ones
        onCreate(db);
    } catch (SQLException e) {
        Log.e(OrmLiteDBProvider.class.getName(), "Can't drop databases", e);
        throw new RuntimeException(e);
    }
}

I think it's something simple I'm missing.

Thanks in advance for your effort.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青巷忧颜 2024-10-11 07:40:56

好的,我看到了这个问题,不幸的是,它也存在于示例程序中。在 ORMLite 帮助程序类中,onUpgrade 方法应使用:

onCreate(db, connectionSource);

而不是调用子类的以下内容:

onCreate(db);

I'我们在 HelloAndroid 示例程序中重现了这个问题,该问题已得到修复。我还在 ORMLite 代码的 Android 端的 OrmLiteSqliteOpenHelper 基类中正确修复了此问题。很抱歉出现这个问题。

Ok, I see the problem and it exists, unfortunately, in the sample program as well. In the ORMLite helper class, the onUpgrade method should use:

onCreate(db, connectionSource);

instead of the following which is calling the subclass:

onCreate(db);

I've reproduced this problem in the HelloAndroid example program which has been fixed. I've also fixed this properly in the OrmLiteSqliteOpenHelper base class in the Android side of the ORMLite code. Sorry for the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文