Android BULK INSERT data into SQLite database

Source: Internet
Author: User
Tags bulk insert deprecated

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 void Inertorupdatedatebatch (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 transaction db.settransactionsuccessful () is committed when the transaction is ended; catch (Exception e) {e.printstacktrace ();} finally {//End transaction db.endtransaction ();d b.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.
Db.begintransaction (); Manually set start transaction for (Contentvalues v:list) {Db.insert ("bus_line_station", null, V);} Db.settransactionsuccessful (); Set transaction success, do not set automatically rollback does not commit db.endtransaction (); Processing Complete db.close ()
3. Using Inserthelper classThis class has been deprecated in API 17.
Inserthelper ih = new Inserthelper (db, "Bus_line_station");d b.begintransaction (); final int directcolumnindex = Ih.getcolumnindex ("direct"); final int linenamecolumnindex = Ih.getcolumnindex ("Line_name"); final int snocolumnindex = Ih.getcolumnindex ("Sno"), final int stationnamecolumnindex = Ih.getcolumnindex ("Station_Name"), try {for station s: Buslines) {Ih.prepareforinsert (); Ih.bind (Directcolumnindex, S.direct); Ih.bind (Linenamecolumnindex, s.lineName); Ih.bind (Snocolumnindex, S.sno); Ih.bind (Stationnamecolumnindex, s.stationname); Ih.execute ();} Db.settransactionsuccessful ();} finally {ih.close ();d b.endtransaction ();d b.close ();}
   4. Using SqlitestatementWhen viewing Inserthelper, the official documentation indicates that the class has been deprecated, please use sqlitestatement
String sql = "INSERT into bus_line_station (direct,line_name,sno,station_name) VALUES (?,?,?,?)"; Sqlitestatement stat = db.compilestatement (sql);d b.begintransaction (); for (station Line:buslines) {Stat.bindlong (1, Line.direct); stat.bindstring (2, Line.linename); Stat.bindlong (3, Line.sno); Stat.bindstring (4, line.stationName); Stat.executeinsert ();} Db.settransactionsuccessful ();d b.endtransaction ();d b.close ();
Is the method above 4 in bulk insert 10,000 data consumption time can find the third method takes the shortest time, because the class has been discarded in API17, so the fourth method should be the best method.

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.