Original address: http://www.cnblogs.com/wangmars/p/3914090.html
SQLite, is a lightweight database, is widely used in a lot of embedded products, because the use of very little resources, two of the operation of the way almost and we contact the database is not much, even only hundreds of k he will naturally be favored by the demand, Let's talk about how to read and write to him in such a lightweight database.
Before making a selection of contacts appear if a phone more than 2000 contacts, inserting into the database will be very time-consuming, different phone storage of different number of bars, the amount of storage and the memory of the phone has a great relationship, often depends on the phone memory, The following is the case of large amounts of data to write the SQLite batch query.
SqLite inserts data there are several
The first: Since the Inserthelper class has been deprecated on Android api17, it is based on a previously developed
Inserthelper ih =NewInserthelper (DB, "table name");d b.begintransaction ();Final intColumn 1= ih.getcolumnindex ("Column 1");Final int column 2= Ih.getcolumnindex ("Column 2");Try { for(station S:buslines) {Ih.prepareforinsert (); ih.bind (column 1, corresponding value); ih.bind (column 2, corresponding value); Ih.execute ();} Db.settransactionsuccessful ();} finally{ih.close ();d b.endtransaction ();d b.close ();}
The second type:
Also in the sqlitedatabase.
Public voidInertorupdatedatebatch (list<string>Sqls) {Sqlitedatabase db=getwritabledatabase ();d b.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 endeddb.settransactionsuccessful ();} Catch(Exception e) {e.printstacktrace ();}finally {//End Transactiondb.endtransaction ();d b.close ();}}
The third type: Sqlitedatabase db.insert ("table_name", NULL, contentvalues) can also be bulk inserted
Public voidinsertdata (insert data) {db.begintransaction ();//set up a start transaction manually for(contentvalues v:list) {Db.insert ("Table name",NULL, v);} Db.settransactionsuccessful (); //SET transaction success, do not set automatically rollback does not commitDb.endtransaction ();//Processing Completedb.close ()}
The fourth kind: sqlitestatement personal prefer this way, the data processing look very clear
String sql = "INSERT into table name (corresponding column) VALUES (?)" = db.compilestatement (sql);d b.begintransaction (); for (DataSet) { ///loop the data to be inserted}db.settransactionsuccessful ();d b.endtransaction ();d b.close ();
Summary: The above several methods are used in the database of the transaction this thing, SQLite statement in which only walk once, the other is the data loop into the database objects, so than before with the object inserted, and then use for in the peripheral loop fast do not know how many times, before inserting more than 2000 data 300 milliseconds , it is also very fast for tens of thousands of data.
Source: http://www.cnblogs.com/wangmars/p/3914090.html
From for notes (Wiz)
Android SQLite BULK Insert data slow solution (for different Android API versions)