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