Android BULK INSERT data efficiency comparison

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.