android sqlite的事务

* There are two kinds of transaction: implicit transactions and explicit
* transactions.
* </p><p>
* An implicit transaction is created whenever a database operation is requested
* and there is no explicit transaction currently in progress.  An implicit transaction
* only lasts for the duration of the database operation in question and then it
* is ended.  If the database operation was successful, then its changes are committed.
* </p><p>
* An explicit transaction is started by calling {@link #beginTransaction} and
* specifying the desired transaction mode.  Once an explicit transaction has begun,
* all subsequent database operations will be performed as part of that transaction.
* To end an explicit transaction, first call {@link #setTransactionSuccessful} if the
* transaction was successful, then call {@link #end}.  If the transaction was
* marked successful, its changes will be committed, otherwise they will be rolled back.
* </p><p>
* Explicit transactions can also be nested.  A nested explicit transaction is
* started with {@link #beginTransaction}, marked successful with
* {@link #setTransactionSuccessful}and ended with {@link #endTransaction}.
* If any nested transaction is not marked successful, then the entire transaction
* including all of its nested transactions will be rolled back

* when the outermost transaction is ended.

for example:----------------------

db.execSQL("update person set amount=amount-10 where personid=?", new Object[]{1});  

or.....

  1.  db.beginTransaction();  
  1.     try  
  1.     {  
  1.         db.execSQL("update person set amount=amount-10 where personid=?", new Object[]{1});  
  1.         db.execSQL("update person set amount=amount+10 where personid=?", new Object[]{2});  
  1.         //设置事务标志为成功,当结束事务时就会提交事务  
  1.         db.setTransactionSuccessful();  
  2.     }  
  1.    catch(Exception e){
  2.         throw(e);
  1.     }
  1.     finally  
  2.     {  
  1.         //结束事务  
  1.         db.endTransaction();  
  2. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值