setTransactionSuccessful() 设置事务成功标记
endTransaction() 结束事务.
事务结束的时候,会把最后一个成功标记之前的操作提交,成功标记之后的操作回滚
public void remit(int fromId,int toId,int amount) { sqliteDatabase db = helper.getWritableDatabase(); try { db.beginTransaction(); db.execsql("UPDATE account SET balance=balance-? WHERE _id=?",new Object[] { amount,fromId }); System.out.println(1 / 0); db.execsql("UPDATE account SET balance=balance-? WHERE _id=?",toId }); db.setTransactionSuccessful(); } finally { db.endTransaction(); db.close(); } }原文链接:https://www.f2er.com/sqlite/200352.html