Transactional operations in SQLite

Source: Internet
Author: User

About SQLite transaction can solve some problems, such as you want to insert two data, you can insert two data as the same transaction, so if the second data is wrong, then automatically perform the rollback operation, the first data will not be inserted successfully, to ensure the synchronization of data!


To give a practical example:

Application initialization requires a lot of data to be inserted into SQLite, and the use of the For+insert method alone causes the application to respond slowly, because when SQLite inserts the data, the default statement is a transaction, and the number of times the disk operation is there. My application initial 5,000 records that is to read and write disk operations 5,000 times.
And there is no guarantee that all data can be inserted simultaneously. (It is possible that part of the insert succeeds, another part fails, and subsequent deletions are made.) Too troublesome)


Workaround:
Add a transaction, insert 5,000 as a transaction

we use SQLite's transactions to control:
db.begintransaction ();//Manually set the start transaction
try{
//Perform a bulk processing operation here, with the following loop insert as an example
For (Collection c:colls) {
Insert (DB, c);
}
db.settransactionsuccessful (); //SET transaction successfully, do not set automatically rollback does not commit
}catch (Exception e) {
mylog.printstacktracestring (e);
}finally{
db.endtransaction ();//Processing complete
}

Description
1. Use the Sqlitedatabase BeginTransaction () method to open a transaction, and the program executes to
The Endtransaction () method checks whether the flag of the transaction is successful if the program executes to
Endtransaction () before calling the Settransactionsuccessful () method to set the flag for the transaction to be successful then mention
The transaction is rolled back if the settransactionsuccessful () method is not called.

Thank you: http://blog.csdn.net/a443453087/article/details/7222247

Transactional operations in SQLite

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.