gpt4 book ai didi

java - Android - Green Dao 多重交易

转载 作者:行者123 更新时间:2023-12-01 22:07:26 24 4
gpt4 key购买 nike

我在 greendao 中进行多笔交易时遇到一些问题例如,我已经有两个包含 name book 和 type_book 的表。

我已经有这样的代码:

    SQLiteDatabase db = bookMasterDao.getDatabase();
db.beginTransaction();

try {
bookMasterDao.insert(bookMaster);
idBook = bookMaster.getId().intValue();
db.setTransactionSuccessful();
} catch (Exception ex) {
System.out.println("Error insert book master " + ex);
} finally {
db.endTransaction();
}



SQLiteDatabase dbTypeBook = typeBookMasterDao.getDatabase();
dbTypeBook.beginTransaction();

try {
typeBookMasterDao.insert(bookMaster);
dbTypeBook.setTransactionSuccessful();
} catch (Exception ex) {
System.out.println("Error insert type book" + ex);
} finally {
dbTypeBook.endTransaction();
}

我想要如果事务类型账本插入失败,事务账本主回滚我的意思是取消插入数据账本主。如何进行这样的交易?

任何想法、引用和示例都非常有帮助,谢谢。

最佳答案

我认为你误解了原则。尝试创建数据库,启动事务,用daoMaster包装数据库,使用 session 进行插入,然后设置事务成功。看一下代码。

SQLiteDatabase db = openOrCreateDatabase("db", MODE_PRIVATE, null);
DaoMaster.createAllTables(db, true);

db.beginTransaction();
try {

DaoMaster daoMaster = new DaoMaster(db);
DaoSession daoSession = daoMaster.newSession();

Book book = new Book();
Book2 book2 = new Book2();

//insert on specific session
daoSession.getBookDao().insert(book);

//set some relation to inserted book
book2.setBook(book);

//if this fails, book and book2 won't be in db
//insert withou specific session
daoSession.insert(book2);

db.setTransactionSuccessful();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
db.endTransaction();
db.close();
}

关于java - Android - Green Dao 多重交易,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32412573/

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