安卓& OrmLite:OnUpgrade 失败
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我看到了这个问题,不幸的是,它也存在于示例程序中。在 ORMLite 帮助程序类中,
onUpgrade
方法应使用:而不是调用子类的以下内容:
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:instead of the following which is calling the subclass:
I've reproduced this problem in the
HelloAndroid
example program which has been fixed. I've also fixed this properly in theOrmLiteSqliteOpenHelper
base class in the Android side of the ORMLite code. Sorry for the problem.