A large number of insert transactions are enabled for Android databases, and android transactions are performed.

Source: Internet
Author: User

A large number of insert transactions are enabled for Android databases, and android transactions are performed.

Comparison of the three methods of batch data insertion in android (the time spent on inserting 1 million pieces of data each ):

1. Insert one by one

 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);         }

Time consumed: 106524 ms, that is, 106 s

2. Enable batch transaction insertion by 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 (Exception e) {          e.printStackTrace();          returnfalse;      } finally {          try {              if (null != db) {                  db.endTransaction();                  db.close();              }          } catch (Exception e) {              e.printStackTrace();          }      }      returntrue;  }  

Time consumed: 2968 ms

3. Enable batch transaction insert and use SQLiteStatement

Publicstaticboolean insertBySql (SQLiteOpenHelper openHelper, List <RemoteAppInfo> list) {if (null = 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 + RemoteDB Helper. COL_APP_SOURCE + "," // source + RemoteDBHelper. COL_SOURCE_UNIQUE + "," // PC mac address + RemoteDBHelper. COL_MOBILE_UNIQUE + "," // unique mobile phone ID + RemoteDBHelper. COL_IMEI + "," // mobile phone IMEI + RemoteDBHelper. COL_INSTALL_STATUS + "," // installation status + RemoteDBHelper. COL_TRANSFER_RESULT + "," // transmission status + RemoteDBHelper. COL_REMOTE_RECORD_ID // Unique Identifier + ")" + "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.exe cuteInsert (); 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 consumed: 1365 ms

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.