In-depth Android SQLite transaction processing _android

Source: Internet
Author: User
Tags sqlite


Application initialization requires a lot of data to be injected into the SQLite, and a separate use of the For+insert method causes the application to respond slowly because the default statement of the SQLite when inserting data is a transaction and how many times disk operations there are. The first 5,000 records of my application are 5,000 read and write disk operations.



And there is no guarantee that all data can be plugged in at the same time. (It is possible that part of the insert succeeds, the other part fails, and the follow-up is removed.) Too much trouble)



Workaround:



Add transaction, insert 5,000 as a transaction




We use SQLite transactions for control:

 code as follows:


Db.begintransaction (); Set Start transaction manually





try{



Bulk processing operations



for (Collection c:colls) {



Insert (db, C);



}



Db.settransactionsuccessful (); Set transaction success, no setting will automatically rollback uncommitted.



No database operations between Settransactionsuccessful and Endtransaction



}catch (Exception e) {



Mylog.printstacktracestring (e);



}finally{



Db.endtransaction (); Processing complete



}






One,Use the Sqlitedatabase BeginTransaction () method to open a transaction that checks whether the transaction's flag is successful if the program executes to the Endtransaction () method, if the program executes to Endtransaction () The Settransactionsuccessful () method was previously invoked to set the transaction's flag as successful, then all operations starting from BeginTransaction () would be committed if the settransactionsuccessful () was not invoked Method rolls back the transaction.





Second, the use of examples are as follows: The following two SQL statements are executed in the same transaction.



Java code


 code as follows:

Bank account Transaction Test
public void Payment ()
{
Sqlitedatabase db = Dbopenhelper.getwritabledatabase ();
Open transaction
Db.begintransaction ();
Try
{
Db.execsql ("Update person set amount=amount-10 where personid=?", New Object[]{1});
Db.execsql ("Update person set amount=amount+10 where personid=?", New object[]{2});
Set the transaction flag to succeed and commit the transaction when the transaction is closed
Db.settransactionsuccessful ();
}
catch (Exception e) {
Throw (e);
}
Finally
{
End transaction
Db.endtransaction ();
}
}




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.