我使用ormlite for android并且我有一个扩展OrmLiteSqliteOpenHelper的数据库表类。我在谷歌播放中有一些报道称应用程序因以下原因导致关闭:
android.database.sqlite.SQLiteException: Can't downgrade database from version 3 to 2
用户可能会通过备份等降级。问题是我无法实现SQLiteOpenHelper上存在的onDowngrade方法:
ormlite是否支持降级?这有什么工作吗?至少要避免力量关闭。
答案 0 :(得分:1)
有趣。因此,在API 11中添加了onDowngrade(...)
方法。我不能只将它的支持添加到ORMLite中。不幸的是,这意味着您必须制作自己的onDowngrade
方法,该方法与onUpgrade(...)
中的OrmLiteSqliteOpenHelper
相同。像跟随标志:
public abstract void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion,
int newVersion) {
// your code goes here
}
public final void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
ConnectionSource cs = getConnectionSource();
/*
* The method is called by Android database helper's get-database calls when Android detects that we need to
* create or update the database. So we have to use the database argument and save a connection to it on the
* AndroidConnectionSource, otherwise it will go recursive if the subclass calls getConnectionSource().
*/
DatabaseConnection conn = cs.getSpecialConnection();
boolean clearSpecial = false;
if (conn == null) {
conn = new AndroidDatabaseConnection(db, true);
try {
cs.saveSpecialConnection(conn);
clearSpecial = true;
} catch (SQLException e) {
throw new IllegalStateException("Could not save special connection", e);
}
}
try {
onDowngrade(db, cs, oldVersion, newVersion);
} finally {
if (clearSpecial) {
cs.clearSpecialConnection(conn);
}
}
}
有关onDowngrade(...)
method的更多信息,请参阅以下内容:
public void onDowngrade (SQLiteDatabase db, int oldVersion, int newVersion);
引用javadocs:
在需要降级数据库时调用。这与onUpgrade(SQLiteDatabase,int,int)方法严格相似,但只要当前版本比请求的版本更新,就会调用它。但是,此方法不是抽象的,因此客户不必实施它。如果没有重写,默认实现将拒绝降级并抛出SQLiteException
另见: