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