有趣的。所以这个onDowngrade(...)
方法是在 API 11 中添加的。我不能只在 ORMLite 中添加对它的支持。不幸的是,这意味着您将不得不制作自己的onDowngrade
方法,该方法与onUpgrade(...)
in相同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(...)
方法的更多信息,请参见下文:
public void onDowngrade (SQLiteDatabase db, int oldVersion, int newVersion);
引用javadocs:
当数据库需要降级时调用。这与 onUpgrade(SQLiteDatabase, int, int) 方法非常相似,但只要当前版本比请求的版本新,就会调用它。但是,此方法不是抽象的,因此客户不一定要实现它。如果没有被覆盖,默认实现将拒绝降级并抛出 SQLiteException
另见:
无法将数据库从版本 2 降级到 1