Compare the 3 ways in which data is inserted in bulk in Android (the time it takes to insert 1W data):
1. One insert
Publicstaticboolean Insert (Sqliteopenhelper openhelper, Remoteappinfo appInfo) {if(NULL= = AppInfo) {returntrue; } Sqlitedatabase db =NULL;Try{db = Openhelper.getwritabledatabase (); Contentvalues values = Appinfo.getcontentvalues ();return-1! = Db.insert (Remotedbhelper.table_app_remote,NULL, values); }Catch(Exceptione) {e.printstacktrace (); }finally{if(NULL! = db) {db.close (); }} Returnfalse; } for(Remoteappinfo remoteappinfo:List) {Remotedbutil.insert (helper, remoteappinfo); }
Time consuming: 106524ms, i.e. 106s
2. Open transaction Bulk INSERT, using the Insert (string table, String nullcolumnhack, Contentvalues values) method in Sqlitedatebase
Publicstaticboolean Insert (Sqliteopenhelper openhelper,List<RemoteAppInfo>List) {Boolean result =true;if(NULL==List||List. Size () <=0) {returntrue; } Sqlitedatabase db =NULL;Try{db = Openhelper.getwritabledatabase (); Db.begintransaction (); for(Remoteappinfo remoteappinfo:List) {Contentvalues values = remoteappinfo.getcontentvalues ();if(Db.insert (Remotedbhelper.table_app_remote,NULL, values) <0) {result =false; Break; } }if(Result) {db.settransactionsuccessful (); } }Catch(Exceptione) {e.printstacktrace (); Returnfalse; }finally{Try{if(NULL! = db) {db.endtransaction (); Db.close (); } }Catch(Exceptione) {e.printstacktrace (); }} returntrue; }
Time: 2968ms
3. Open transaction Bulk INSERT, use Sqlitestatement
Publicstaticboolean Insertbysql (Sqliteopenhelper openhelper, list<remoteappinfo> List) {if (n ull = = Openhelper | | NULL = = List | | List. Size() <=0) {Returnfalse; } Sqlitedatabase db = null; try {db = Openhelper. Getwritabledatabase(); String sql ="INSERT INTO"+ Remotedbhelper. TABLE_app_remote +"("+ Remotedbhelper. COL_pkg_name +","Package name + Remotedbhelper. COL_user_account +","Account + Remotedbhelper. COL_app_source +","SOURCE + Remotedbhelper. COL_source_unique +","PC MAC address + remotedbhelper. COL_mobile_unique +","Phone Unique ID + remotedbhelper. COL_imei +","Phone IMEI + remotedbhelper. COL_install_status +","Installation Status + Remotedbhelper. COL_transfer_result +","Transmit Status + remotedbhelper. COL_REMOTE_RECORD_ID//Unique ID +") "+"VALUES (?,?,?,?,?,?,?,?,?)"; Sqlitestatement stat = db. Compilestatement(SQL); Db. BeginTransaction(); for (Remoteappinfo remoteappinfo:list) {stat. bindstring(1, Remoteappinfo. Getpkgname()); Stat. bindstring(2, Remoteappinfo. Getaccount()); Stat. Bindlong(3, Remoteappinfo. Getfrom()); Stat. bindstring(4, Remoteappinfo. Getfromdevicemd5 ()); Stat. bindstring(5, Remoteappinfo. Getmobliemd5 ()); Stat. bindstring(6, Remoteappinfo. Getimei()); Stat. Bindlong(7, Remoteappinfo. Getinstallstatus()); Stat. Bindlong(8, Remoteappinfo. Gettransferresult()); Stat. bindstring(9, Remoteappinfo. Getrecordid()); Long result = Stat. Executeinsert(); if (Result <0) {Returnfalse; }} DB. Settransactionsuccessful(); } catch (Exception e) {E. Printstacktrace(); Returnfalse; } finally {try {if (null! = db) {db. Endtransaction(); Db. Close(); }} catch (Exception e) {E. Printstacktrace(); }} returntrue; }
Time: 1365ms
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Database BULK INSERT Transaction Open