public void transaction() {
SQLiteDatabase db=dbOpenHelper.getReadableDatabase();
if(db.isOpen()){
try {
//开启数据库的事物
db.beginTransaction();
//给张三设置100块钱的账户
db.execSQL("update person set account=? where name=?",new Object[]{1000,"zhangsan97"});
//
//把张的账户扣除200块钱
db.execSQL("update person set account=account-? where name=?",new Object[]{200,"zhangsan97"});
//出现了异常
//把张三的钱给李四
//初始化李四账户为0
db.execSQL("update person set account=? where name=?",new Object[]{0,"lisi"});
db.execSQL("update person set account=account+? where name=?",new Object[]{200,"lisi"});
//显示的设置事物是否成功
db.setTransactionSuccessful();
} catch (Exception e) {
}
finally{
db.endTransaction();
db.close();
}
}
SQLiteDatabase db=dbOpenHelper.getReadableDatabase();
if(db.isOpen()){
try {
//开启数据库的事物
db.beginTransaction();
//给张三设置100块钱的账户
db.execSQL("update person set account=? where name=?",new Object[]{1000,"zhangsan97"});
//
//把张的账户扣除200块钱
db.execSQL("update person set account=account-? where name=?",new Object[]{200,"zhangsan97"});
//出现了异常
//把张三的钱给李四
//初始化李四账户为0
db.execSQL("update person set account=? where name=?",new Object[]{0,"lisi"});
db.execSQL("update person set account=account+? where name=?",new Object[]{200,"lisi"});
//显示的设置事物是否成功
db.setTransactionSuccessful();
} catch (Exception e) {
}
finally{
db.endTransaction();
db.close();
}
}