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