2016-10-21 6 views
1

Мне нужно отбросить мои таблицы базы данных при вызове метода onDowngrade класса OrmLiteSqliteOpenHelper.Падающие таблицы onDowngrade метод Ormlite

@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() вызывается recusively.

Любой совет по этому вопросу? Я просто хочу оставить свои таблицы и создать их снова, независимо от того, является ли код версии базы данных более новым или более старым.

ответ

2

Вслед за предыдущий ответ от автора OrmLite: OrmLiteSqliteOpenHelper onDowngrade

Вы можете сохранить источник соединения и передать его в таблицу падения называет так:

@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); 
} 
+0

Nice, я заканчивал закреплять из 'onConfigure (SQLiteDatabase)' сохранение устаревшей версии базы данных в переменной и использование этого для удаления таблиц за пределами жизненного цикла DatabaseHelper. Но я думаю, что ваше решение - это путь. – 4gus71n

 Смежные вопросы

  • Нет связанных вопросов^_^