Android & OrmLite: OnUpgrade fails
문제
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.
해결책
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.