这是我使用 ORMLITE 保存在 sqlite 数据库中的模型类。
public class Site{
...
...
Collection<Visit> visits;
}
public class Visit{
...
...
Collection<Pic> pics;
}
public class Pic{
...
...
}
现在在一个 View 中,我从这三个表中添加、编辑或删除。我的 View 中有一个按钮可以取消更改(添加、编辑、删除)。所以我需要回滚到数据库的先前状态。
我如何使用 ormlite 和 android 实现回滚到三个表的特定状态?
我已经阅读了关于 DAO.SetAutoCommit() 和 startThreadConnection() 方法的 ormlite 文档。我想我可以通过使用这两个来做到这一点。但无法弄清楚如何使用它们。请建议我该怎么做。
这是我的 DBHelper 类:
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
// name of the database file
private static final String DATABASE_NAME = "Test.db";
private static final int DATABASE_VERSION = 1;
private Dao<Site, Integer> siteListDao = null;
private Dao<Visit, Integer> visitDao= null;
private Dao<Pic,Integer> picDao=null;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database,ConnectionSource connectionSource) {
try {
TableUtils.createTable(connectionSource, Site.class);
TableUtils.createTable(connectionSource, Visit.class);
TableUtils.createTable(connectionSource, Pic.class);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
throw new RuntimeException(e);
} catch (java.sql.SQLException e) {
e.printStackTrace();
}
}
public Dao<Visit, Integer> getVisitDao() {
if (null == visitDao) {
try {
visitDao = getDao(Visit.class);
}catch (java.sql.SQLException e) {
e.printStackTrace();
}
}
return visitDao;
}
.....
.....
最佳答案
嗯,不知道有什么方法可以自动为您执行此操作。如果你真的想自动完成,你也可以查看交易。他们有内置的控制来回滚失败。不完全是你想要的,但也许可以调整。
您也可以更手动地执行此操作,并在内存中保存 1 个(最后一个)对象,即您要删除的对象。如果有人想“撤消”更改,您只需使用 ORMLite 重新添加该对象即可。可能比其他选项容易得多。
关于android - 需要将数据库回滚到以前的状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15635028/