Android SQLite Transaction Processing

Source: Internet
Author: User

Recently, an android project encountered a problem. During app initialization, a large amount of data needs to be inserted into SQLite in batches. The for + insert method alone causes slow application response, because when SQLite inserts data, a default statement is a transaction, and disk operations are performed as many data records as possible. The first 5000 records of my application are 5000 read/write operations on the disk.
It is not guaranteed that all data can be inserted at the same time. (Some of them may be successfully inserted, and the other part may fail to be deleted later. Too troublesome)

Solution:

Add transaction processing and insert 5000 records as a transaction

We use SQLite transactions for control:
     DB. begintransaction ();// Manually set start transaction
     Try {
       // Batch Processing
       For (collection C: colls ){
         Insert (dB, C );
       }
       DB. settransactionsuccessful(); // Sets whether the transaction is successfully processed. If this parameter is not set, automatic rollback is performed and no commit is performed.
      } Catch (exception e ){
        Mylog. printstacktracestring (E );
      } Finally {
        DB. endtransaction (); // processing completed
      }

To:

1.Use sqlitedatabaseBegintransaction ()Method To start a transaction, the program runsEndtransaction ()Method will check whether the transaction flag is successful, if the program is called before the endtransaction ()Settransactionsuccessful()If the flag of the transaction is set to successful, the transaction is committed. If settransactionsuccessful is not called()
Method to roll back the transaction.

2.Example: The following two SQL statements are executed in the same transaction.

Java code
  1. // Bank Account Transaction Test 
  2. PublicVoidPayment () 
  3.     SqlitedatabaseDB=Dbopenhelper. getwritabledatabase (); 
  4.     // Start the transaction 
  5.     DB. begintransaction (); 
  6.     Try 
  7.     { 
  8.         Db.exe csql ("UpdatePersonSetAmount = amount-10WherePersonid =? ",NewObject [] {1 }); 
  9.         Db.exe csql ("UpdatePersonSetAmount = Amount + 10WherePersonid =? ",NewObject [] {2 }); 
  10.         // Set the transaction flag to successful. When the transaction ends, the transaction will be committed. 
  11.         DB. settransactionsuccessful(); 
  12.     } 
  13.     Finally 
  14.     { 
  15.         // End the transaction 
  16.         DB. endtransaction (); 
  17.     } 
  18. }

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.