How Android uses SQLite to implement transactions

Source: Internet
Author: User

Copy codeThe Code is as follows: public void payment (){
SQLiteDatabase db = dbOpenHelper. getReadableDatabase ();
Db. beginTransaction (); // start the transaction
Try {
Db.exe cSQL ("update person set amount = amount-10 where personid = 2 ");
Db.exe cSQL ("update person set amount = amount + 10 where personid = 5 ");
Db. setTransactionSuccessful (); // set the transaction flag to true. Calling this method will commit the transaction when the endTransaction () method is executed. If this method is not called, it will be executed to endTransaction () method to roll back the transaction.
} Catch (SQLException e ){
} Finally {
Db. endTransaction ();
}
// Terminate the transaction in two cases: commit, rollback,
// The transaction commit or rollback is determined by the transaction flag. If the transaction flag is true, the transaction will be committed. Otherwise, the rollback is performed. By default, the transaction flag is false.
}

PS: similar to the transfer function, two operations are completed in the same transaction.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.