Sqlite批量插入大資料的例子

來源:互聯網
上載者:User

這兩天被資料庫插入批量資料折磨的有點抓狂還好找到瞭解決方法,話不多說,直接看下面兩部分代碼:

SQLiteDatabase db = dbHelper.getWritableDatabase();
//         LogUtils.i("開始解析*****************"+ new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss").format(new Date()));
        for (int i = 0; i < messageArray.length(); i++) {
           try {
            JSONObject messageObject = (JSONObject) messageArray.get(i);
            int stamp = JSONUtils.getInt(messageObject, "nTime", 0);
                        String dbLon = JSONUtils.getString(messageObject, "dbLon", "");
            String dbLat = JSONUtils.getString(messageObject, "dbLat", "");
             /******************** 資料庫事務開始 **************************/
                db.beginTransaction();
            db.execSQL("insert into waybaby(stamp,lontude,lantude) values(?,?,?)",new Object[] { stamp, dbLon,dbLat });
                        db.setTransactionSuccessful();
                        db.endTransaction();
             /******************** 資料庫事務結束 **************************/
            tranceList.add(Double.parseDouble(dbLat));
            tranceList.add(Double.parseDouble(dbLon));
            LatLng latLng = new LatLng(Double.parseDouble(dbLat), Double.parseDouble(dbLon));
            latLngline.add(latLng);
            } catch (JSONException e) {
                e.printStackTrace();
              }
            }
              dismissProgressDialog();
//            LogUtils.i("解析結束*****************"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                      // 畫路線軌跡
              onMapLoaded();

這段代碼是用來解析資料的,然後插入到本機資料庫裡面,總共:1000 條資料,用時:10秒~~15秒。十五秒的等待,花兒都謝了。請看下面修改後的代碼:


SQLiteDatabase db = dbHelper.getWritableDatabase();
//      LogUtils.i("開始解析*****************"+ new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss").format(new Date()));
          for (int i = 0; i < messageArray.length(); i++) {
            try {
                 JSONObject messageObject = (JSONObject) messageArray.get(i);
                 int stamp = JSONUtils.getInt(messageObject, "nTime", 0);
                 String dbLon = JSONUtils.getString(messageObject, "dbLon", "");
                 String dbLat = JSONUtils.getString(messageObject, "dbLat", "");
                 LocusEntity locusEntity = new LocusEntity(stamp, dbLon, dbLat);
                 locusEntities.add(locusEntity);
                 tranceList.add(Double.parseDouble(dbLat));
                 tranceList.add(Double.parseDouble(dbLon));
                 LatLng latLng = new LatLng(Double.parseDouble(dbLat), Double.parseDouble(dbLon));
                 latLngline.add(latLng);
               } catch (JSONException e) {
                  e.printStackTrace();
              }
            }
//         LogUtils.i("開始插入資料*****************"+ new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss").format(new Date()));
           db.beginTransaction();
           String sql = "insert into waybaby(stamp,lontude,lantude) values(?,?,?)";
            for (LocusEntity locusEntity : locusEntities) {
            SQLiteStatement stat = db.compileStatement(sql);
            stat.bindLong(1, locusEntity.getStamp());
            stat.bindString(2, locusEntity.getLontude());
            stat.bindString(3, locusEntity.getLantude());
            stat.executeInsert();
             }
               db.setTransactionSuccessful();
               db.endTransaction();
              db.close();
//            LogUtils.i("插入資料完畢*****************"+ new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss").format(new Date()));
              dismissProgressDialog();
//            LogUtils.i("解析結束*****************"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                       // 畫路線軌跡
              onMapLoaded();

細心的你,肯定會發現,插入資料的操作,被拿到了for迴圈的外面,同時插入資料庫的操作也改變了,多了一大段代碼,從一行變成了十幾行,但是效率提高的不是一點兩點。這段代碼執行的時間: 不到 1 秒,列印的時間資訊,你會發現,時間不用1秒 就擷取成功了。我列印的時間就不到 1 秒。比之前的效率快的將近十倍,你敢信? 事實就是如此。至於那段代碼是什麼意思。請自行百度,自己學習。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.