Transactions for the Android database

Source: Internet
Author: User

transaction: Executes multiple SQL statements, either executing successfully at the same time, failing at the same time, not succeeding, or failing

Bank Transfer


//Click the button to execute the method
Public void Transtation (View v) {
//1. Creating an object for a helper class
bankopenhelper bankopenhelper = new Bankopenhelper (this);
//2. Call the database helper class object Getreadabledatabase Create the database, initialize the table data, get a Sqlitedatabase object to do the transfer (SQL statement)
Sqlitedatabase db = Bankopenhelper.getreadabledatabase ();
//3. Transfer, reduce John Doe's money by 200, Zhang Sanga
db.begintransaction ();//Open a database transaction
try {
db.execsql ("Update account set money= money-200 where Name=?", New string[]{"John Doe"});
int i = 100/0;//simulates an exception
db.execsql ("Update account set money= money+200 where Name=?", New string[]{"Zhang San"});

db.settransactionsuccessful ();//The SQL statements in the marked transaction are all executed successfully
} finally {
db.endtransaction ();//Determines whether the token of the transaction is successful, and if unsuccessful, the SQL statement executed before the rollback error
        }
    }

Transactions for the Android database

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.