Android BULK INSERT data into SQLite database

Source: Internet
Author: User

   AndroidWhen SQLite inserts the data by default a statement is a transaction, so if there are tens of thousands of data inserted, then you need to do tens of thousands of insertions, operation speed imaginable.  So when inserting data into Android, using BULK insert can greatly improve the insertion speed. Sometimes need to put some data into the application, commonly used in the following 2 ways: One directly copy the production of SQLite database files, the second is the use of system-provided DatabaseAnd then insert the data in bulk. I prefer to use the second approach: using a system-created database, and then inserting the data in bulk. There are a lot of ways to insert data in bulk, so that's faster, and a demo compares the insertion speed of each method. 1. Using Db.execsql (SQL)This is where the data to be inserted is stitched into an executable SQL statement, and then the Db.execsql (SQL) method is called to perform the insert.
 Public voidInertorupdatedatebatch (list<string>Sqls) {Sqlitedatabase db=getwritabledatabase (); Db.begintransaction ();Try{ for(String sql:sqls) {db.execsql (SQL);} //The transaction flag is set to be successful, and the transaction is committed when the transaction is ended    db.settransactionsuccessful ();} Catch(Exception e) {e.printstacktrace ();}finally {//End Transaction    db.endtransaction ();  Db.close (); }}

2. Using Db.insert ("table_name", NULL, Contentvalues)

This is to encapsulate the data to be inserted into the Contentvalues class, and then call the Db.insert () method to perform the insertion.
// set up a start transaction manually  for (contentvalues v:list) {Db.insert ("bus_line_station"null  // SET transaction success, do not set automatically rollback does not commit // processing completed db.close ();

Android BULK INSERT data into SQLite database

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.