Android学习_18_使用事务操作SQLite数据库

以转账为例:

1> update person set amount=amount-10 where personid = 1;

2>update person set amount = amount+10 where personid = 2;

SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
try{
db.beginTransaction();//开启事务
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
}finally{
db.endTransaction();//结束事务,有两种情况:commit,rollback,
}
//事务的提交或回滚是由事务的标准决定的,如果事务的标志为True,事务就会提交,否则回滚,默认情况下事务的标志为False。




你可能感兴趣的:(sqlite,android,Android)