SQLite (2, business)

Source: Internet
Author: User

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)

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.