Java 放低品位方法或MLite上的表格
当调用Java 放低品位方法或MLite上的表格,java,android,ormlite,Java,Android,Ormlite,当调用OrmLiteSqliteOpenHelper类的ondownggrade方法时,我需要删除数据库表 @Override public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) { try { Log.i(DatabaseHelper.class.getName(), "onUpgrade");
OrmLiteSqliteOpenHelper
类的ondownggrade
方法时,我需要删除数据库表
@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
方法抛出异常,表示正在重复调用getWritableDatabase()
方法
有什么提示吗?我只想删除我的表并重新创建它们,不管数据库版本代码是新的还是旧的。继OrmLite作者之前的回答之后: 您可以保存连接源并将其传递到drop表调用中,如下所示:
@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);
}
很好,我最终连接了
onConfigure(SQLiteDatabase)
将旧数据库版本保存在一个变量中,并使用该变量将表放到DatabaseHelper
生命周期之外。但我认为你的解决办法是正确的。