在降级方法 Ormlite 上删除表

Drop tables onDowngrade method Ormlite

我需要在调用 OrmLiteSqliteOpenHelper class 的 onDowngrade 方法时删除我的数据库表。

@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
    try {
        Log.i(DatabaseHelper.class.getName(), "onUpgrade");
        TableUtils.dropTable(connectionSource, DbFeedJsonRow.class, true);
        TableUtils.dropTable(connectionSource, DbEventJsonRow.class, true); //TODO REMOVE
        TableUtils.dropTable(connectionSource, DbTeamJsonRow.class, true);
        TableUtils.dropTable(connectionSource, DbFavoritePlayerDTO.class, true);
        TableUtils.dropTable(connectionSource, DbAssetDTO.class, true);
        TableUtils.dropTable(connectionSource, DbTaskDTO.class, true);
        TableUtils.dropTable(connectionSource, DbEventDTO.class, true);
        // after we drop the old databases, we create the new ones
        onCreate(db, connectionSource);
    } catch (SQLException e) {
        Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
        throw new RuntimeException(e);
    }
}

@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    try {
        Log.i(DatabaseHelper.class.getName(), "onDowngrade");
        TableUtils.dropTable(connectionSource, DbFeedJsonRow.class, true);
        TableUtils.dropTable(connectionSource, DbEventJsonRow.class, true); //TODO REMOVE
        TableUtils.dropTable(connectionSource, DbTeamJsonRow.class, true);
        TableUtils.dropTable(connectionSource, DbAssetDTO.class, true);
        TableUtils.dropTable(connectionSource, DbTaskDTO.class, true);
        TableUtils.dropTable(connectionSource, DbEventDTO.class, true);
        // after we drop the old databases, we create the new ones
        onCreate(db, connectionSource);
    } catch (SQLException e) {
        Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
        throw new RuntimeException(e);
    }
}

onUpgrade 工作正常,但是 onDowngrade 方法抛出一个异常,表明正在调用 getWritableDatabse() 方法。

有什么建议吗?我只想删除我的表并重新创建它们,无论数据库版本代码是新的还是旧的。

跟进 OrmLite 作者之前的回答:OrmLiteSqliteOpenHelper onDowngrade

您可以保存连接源并将其传递到 drop table 调用中,如下所示:

@Override
public 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 {
        dropTables(cs);
        createTables(cs);
    } catch (SQLException e) {
        // log something
    } finally {
        if (clearSpecial) {
            cs.clearSpecialConnection(conn);
        }
    }
}

private void createTables(ConnectionSource connectionSource) throws SQLException {
    TableUtils.createTable(connectionSource, YourTableObject.class);
}

public void dropTables(ConnectionSource connectionSource) throws SQLException{
    TableUtils.dropTable(connectionSource, YourTableObject.class, true);
}