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.
There are several ways to mix SqLite data
The first: Since the Inserthelper class has been deprecated on Android api17, it is based on a previously developed
New Inserthelper (DB, "table name");d b.begintransaction (); Final int column 1= ih.getcolumnindex ("column 1"); Final int column 2 = Ih.getcolumnindex ("column 2"); Try {for-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 void // Manually set start transaction for (contentvalues v:list) {db.insert (null // SET transaction successfully, do not set automatically rollback does not commit // processing complete db.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.