gpt4 book ai didi

java - 在降级方法 Ormlite 上删除表

转载 作者:行者123 更新时间:2023-11-29 19:30:10 25 4
gpt4 key购买 nike

OrmLiteSqliteOpenHelper 类的 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

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

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

关于java - 在降级方法 Ormlite 上删除表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40181720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com