SQLite by default each execution statement is a transaction, so increasing transactions in addition to atomic protection, but also improve performance, reduce disk operations. Using the SQLite BeginTransaction () method, you can open a transaction that executes to the Endtransaction () method to check whether the flag for the transaction is successful, or to roll back the transaction if it succeeds.
When an application needs to commit a transaction, it must use the Settransactionsuccessfull () method before the program to Endtransaction () method to set the transaction flag as successful. If the transaction flag is not set if settransactionsuccessful () is not called, the default rollback is rolled back.
The program examples are as follows:
Sqlitedatabase db = Openorcreatedatabase ("Demo.db", This. Mode_private,NULL); Db.execsql ("CREATE TABLE IF not EXISTS users (_id INTEGER PRIMARY KEY autoincrement, username varchar, password varchar)"); //Open Transactiondb.begintransaction (); Try{db.execsql ("INSERT into Users VALUES (NULL, ' fredric1 ', ' fredricpassword1 ')"); Db.execsql ("INSERT into Users VALUES (NULL, ' fredric2 ', ' Fredricpassword2 ')"); Db.execsql ("INSERT into Users VALUES (NULL, ' fredric3 ', ' Fredricpassword3 ')"); Db.execsql ("INSERT into Users VALUES (NULL, ' fredric4 ', ' Fredricpassword4 ')"); //The transaction flag is set to be successful, and the transaction is committed when the transaction is endeddb.settransactionsuccessful (); } finally { //End Transactiondb.endtransaction (); Cursor Cursor= Db.rawquery ("SELECT * from users",NULL); while(Cursor.movetonext ()) {log.i (tag_activity, cursor.getstring (cursor.getcolumnindex ("Username"))); LOG.I (Tag_activity, Cursor.getstring (Cursor.getcolumnindex ("Password"))); } cursor.close (); } }
SQLite (2, business)