Android SQLite Transaction Processing

Source: Internet
Author: User

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 (); Set up a start transaction manually
try{
//Bulk processing operations
For (Collection c:colls) {
Insert (DB, c);
            }
db.settransactionsuccessful ();//Set transaction success, do not set automatically rollback does not commit
}catch (Exception e) {
mylog.printstacktracestring (e);
}finally{
db.endtransaction ();//Processing complete
           }

Turn:

1. use Sqlitedatabase's BeginTransaction () method to open a transaction, the program executes to endtransaction () The Endtransaction method checks whether the flag for the transaction is successful, if the program executes to the settransactionsuccessful ( ) method before the call to set the transaction flag as successful commits the transaction, If the Settransactionsuccessful () method is not called, the transaction is rolled back.

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

Java Code
    1. Bank account Transaction Testing
    2. public void Payment ()
    3. {
    4. Sqlitedatabase db = Dbopenhelper.getwritabledatabase ();
    5. Open transaction
    6. Db.begintransaction ();
    7. Try
    8. {
    9. Db.execsql ("Update person set amount=amount-10 where personid=?", New Object[]{1});
    10. Db.execsql ("Update person set amount=amount+10 where personid=?", New object[]{2});
    11. The transaction flag is set to be successful, and the transaction is committed when the transaction is ended
    12. Db.settransactionsuccessful ();
    13. }
    14. Finally
    15. {
    16. End transaction
    17. Db.endtransaction ();
    18. }
    19. }

Android SQLite Transaction Processing

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.