1. <i id="6fn9r"><bdo id="6fn9r"></bdo></i>

    2. <u id="6fn9r"><sub id="6fn9r"></sub></u>
    3. <u id="6fn9r"><bdo id="6fn9r"></bdo></u>
        最新開班
        課程名稱 報名截止 報名人數
        高中起點學習班 即將開班 熱招中
        企業委培訂制班 即將開班 熱招中
        私人名師輔導班 即將開班 熱招中
        待業速找工作班 即將開班 熱招中
        專家大課堂
        accp
        accp
        男生
        女生學什么好?女生學什么有前途?
        高中落榜怎么辦?高中生可以學軟件么?
        專職、再就業,讓你的生涯不再與苦累相伴。
        聯系方式
        地圖

        免費咨詢:400-696-8028

        報名電話:0731-88122069

        學校地址:長沙市開福區金霞跨境產貿城北大青鳥校區

        Android批量插入數據效率對比
        發布時間:2015-10-01

         1、*個*個插入

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        /**
             * 向表中插入數據
             *
             * @param openHelper
             * @param appInfo
             * @return
             */
            public static boolean insert(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);
                        }

        耗時:106524ms,也就是106s

         

        2、 開啟事務批量插入,使用

        SqliteDateBase中的

        insert(String table, String nullColumnHack, ContentValues values)

        方法

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        33
        34
        35
        36
        37
        38
        39
        40
        41
        42
        43
        /**
         * 向表中插入*串數據
         *
         * @param openHelper
         * @param appInfo
         * @return 如果成功則返回true,否則返回flase
         */
        public static boolean insert(SQLiteOpenHelper openHelper,
                List<RemoteAppInfo> list) {
            boolean result = 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;
        }

        耗時:2968ms

         

        3、 開啟事務批量插入,使用

        SQLiteStatement

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        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
        53
        54
        55
        56
        57
        58
        /**
             * 第二種方式批量插入(插入1W條數據耗時:1365ms)
             * @param openHelper
             * @param list
             * @return
             */
            public static boolean insertBySql(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 + ","// 包名
                            + RemoteDBHelper.COL_USER_ACCOUNT + ","// 賬號
                            + RemoteDBHelper.COL_APP_SOURCE + ","// 來源
                            + RemoteDBHelper.COL_SOURCE_UNIQUE + ","// PC mac 地址
                            + RemoteDBHelper.COL_MOBILE_UNIQUE + ","// 手機唯*標識
                            + RemoteDBHelper.COL_IMEI + ","// 手機IMEI
                            + RemoteDBHelper.COL_INSTALL_STATUS + ","// 安裝狀態
                            + RemoteDBHelper.COL_TRANSFER_RESULT + ","// 傳輸狀態
                            + RemoteDBHelper.COL_REMOTE_RECORD_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) {
                            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;
            }

        耗時:1365ms

        上一篇:Android與JavaScript方法相互調用
        下一篇:Android開發 Unity3D基礎 Android Development

        長沙科泰
        長沙科泰
        長沙科泰IT教育
        成年女人AA级毛片免费观看_一级a一级a国产爰片免费免免_亚洲免费看_免费国产一级 片内射视频播

          1. <i id="6fn9r"><bdo id="6fn9r"></bdo></i>

          2. <u id="6fn9r"><sub id="6fn9r"></sub></u>
          3. <u id="6fn9r"><bdo id="6fn9r"></bdo></u>