1. One insert
/*** Insert data into the table * *@paramOpenhelper *@paramAppInfo *@return */ Public Static BooleanInsert (Sqliteopenhelper openhelper, Remoteappinfo appInfo) {if(NULL==appInfo) { return true; } sqlitedatabase DB=NULL; Try{db=openhelper.getwritabledatabase (); Contentvalues Values=appinfo.getcontentvalues (); return-1! = Db.insert (Remotedbhelper.table_app_remote,NULL, values); } Catch(Exception e) {e.printstacktrace (); } finally { if(NULL!=db) {Db.close (); } } return false; } for(Remoteappinfo remoteappinfo:list) {Remotedbutil.insert (helper, remoteappinfo); }
Time consuming: 106524ms, i.e. 106s
2. Open transaction BULK INSERT, use
Sqlitedatebase in the
Insert (string table, String nullcolumnhack, contentvalues values)
Method
/*** Insert a string of data into the table * *@paramOpenhelper *@paramAppInfo *@returnreturns True if successful, otherwise returns flase*/ Public Static BooleanInsert (Sqliteopenhelper openhelper, List<RemoteAppInfo>list) { Booleanresult =true; if(NULL= = List | | List.size () <= 0) { return true; } 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(Exception e) {e.printstacktrace (); return false; } finally { Try { if(NULL!=db) {db.endtransaction (); Db.close (); } } Catch(Exception e) {e.printstacktrace (); } } return true;}
Time: 2968ms
3. Open transaction BULK INSERT, use
Sqlitestatement
/*** Second Way BULK INSERT (insert 1W Data time: 1365ms) *@paramOpenhelper *@paramlist *@return */ Public Static BooleanInsertbysql (Sqliteopenhelper openhelper, List<RemoteAppInfo>list) { if(NULL= = Openhelper | |NULL= = List | | List.size () <= 0) { return false; } 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 Number+ Remotedbhelper.col_app_source + ","//Source+ Remotedbhelper.col_source_unique + ","//PC MAC Address+ Remotedbhelper.col_mobile_unique + ","//Mobile Unique identification+ Remotedbhelper.col_imei + ","//phone IMEI+ Remotedbhelper.col_install_status + ","//Installation Status+ Remotedbhelper.col_transfer_result + ","//Transfer Status+ remotedbhelper.col_remote_record_id//uniquely identifies+ ") +" 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 ()); Longresult =Stat.executeinsert (); if(Result < 0) { return false; }} db.settransactionsuccessful (); } Catch(Exception e) {e.printstacktrace (); return false; } finally { Try { if(NULL!=db) {db.endtransaction (); Db.close (); } } Catch(Exception e) {e.printstacktrace (); } } return true; }
Time: 1365ms
Android BULK INSERT data efficiency comparison