1

私はAndroid 用の ormlite を使用しており、OrmLiteSqliteOpenHelper を拡張するデータベース テーブル クラスがあります。Google Play で、アプリケーションが強制終了したという報告がいくつかありました。

android.database.sqlite.SQLiteException: Can't downgrade database from version 3 to 2

ユーザーはおそらくバックアップなどを介してダウングレードすることになります。問題は、 SQLiteOpenHelper に存在する onDowngrade メソッドを実装できないことです:

ormlite はダウングレードをサポートしていますか? これに対する回避策はありますか?少なくとも強制終了を避けるために。

4

1 に答える 1

1

面白い。そのため、このonDowngrade(...)メソッドはAPI11で追加されました。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(...)方法の詳細については、以下を参照してください。

public void onDowngrade (SQLiteDatabase db, int oldVersion, int newVersion);

javadocsから引用するには:

データベースをダウングレードする必要がある場合に呼び出されます。これはonUpgrade(SQLiteDatabase、int、int)メソッドと厳密に似ていますが、現在のバージョンが要求されたバージョンよりも新しい場合は常に呼び出されます。ただし、この方法は抽象的ではないため、お客様が実装する必要はありません。オーバーライドされない場合、デフォルトの実装はダウングレードを拒否し、SQLiteExceptionをスローします

以下も参照してください。

データベースをバージョン2から1にダウングレードできません

于 2013-03-18T13:15:04.430 に答える