实例教程七:在SQLite中使用事务

系统文件管理器
http://www.eoeandroid.com/thread-128567-1-1.html

android 实现区域截图
http://www.eoeandroid.com/thread-149439-1-1.html

影院选坐 写着玩的
http://www.eoeandroid.com/thread-151980-1-1.html

继上章
1.在数据库表person添加字段amount
2.在Person类中添加相应的amount
3.在PersonService添加payment()方法

public void payment(){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
 }finally{//为何要加try...catch//因为添加了db.setTransactionSuccessful(),若execSQL中出现问题//则不会执行db.endTransaction()
 db.endTransaction(); //结束事务,有2种情况:commit, rollback
 }                //事务的提交或回滚是由事务的标志决定的//如果事务的标志为True,事务就会提交//否则事务就会回滚,默认情况下事务的标志为false
 }

4.初始化amount值,便于调用

public void testUpdateAmount() throws Exception{PersonService service = new PersonService(this.getContext());Person person1 = service.find(1);Person person2 = service.find(2);person1.setAmount(100);person2.setAmount(200);service.update(person1);service.update(person2);}

5.在单元测试类PersonServiceTest中添加测试方法testPayment()

public void testPayment() throws Exception{PersonService service = new PersonService(this.getContext());service.payment();}

 

 

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部