Android数据库SQLite中使用事务

来源:互联网 发布:斯巴鲁森林人STI 知乎 编辑:程序博客网 时间:2024/05/16 09:48
参考链接:http://www.android100.org/html/201502/25/123174.html


SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
db.beginTransaction();// 开启事务
try{
    db.execSQL("update person set amount=amount-10 where personid=1"); 
    db.execSQL("update person set amount=amount+10 where personid=2");
    db.setTransactionSuccessful();// 设置事务的标志为True
}catch(Exception e){
    throw(e);
}finally{
    db.endTransaction();// 结束事务,有两种情况:commit,rollback,
    // 事务的提交或回滚是由事务的标志决定的,如果事务的标志为True,事务就会提交,否则回滚,默认情况下事务的标志为False
}
1 0