Android Database BULK INSERT Transaction Open

Source: Internet
Author: User
Tags bulk insert

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 (exception e) {e.printstacktrace ();} finally {if (null! = db) {db.close ();}} Returnfalse; } for (remoteappinfo remoteappinfo: list) { Remotedbutil.insert (helper, remoteappinfo); }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25

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 =Trueif (NULL = =List | |List.size () <=0) {returntrue;} Sqlitedatabase db =Nulltry {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 (); returnfalse;} finally {try {if (null! = db" {db.endtransaction (); Db.close ();}} catch (exception e) {e.printstacktrace ();}} returntrue;} 
      1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • Ten
    • one
    • 2
    • (
    • )
    • +
    • +
    • /
    • 0
    • +
    • all
    • +
    • +
    • +
    • -
    • 29
    • +
    • +
    • all
    • +
    • +
    • PNS
    • up
    • l>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

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 +","//Mobile 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 ()) .executeinsert () ; if (Result < 0) {returnfalse.settransactionsuccessful () .printstacktrace () .endtransaction () .close () ;} catch (Exception e) {e.printstacktrace () ;} returntrue< Span class= "hljs-comment" >; } 
      1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • Ten
    • one
    • 2
    • (
    • )
    • +
    • +
    • /
    • 0
    • +
    • all
    • +
    • +
    • +
    • -
    • 29
    • +
    • +
    • all
    • +
    • +
    • PNS
    • up
    • i>39
    • 48
    • all
    • /
    • /
    • /
    • /li>
    • --
    • --
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

Time: 1365ms

Android Database BULK INSERT Transaction Open

Related Article

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.