qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Android批量插入數據到SQLite數據庫

          Android中在sqlite插入數據的時候默認一條語句就是一個事務,因此如果存在上萬條數據插入的話,那就需要執行上萬次插入操作,操作速度可想而知。因此在Android中插入數據時,使用批量插入的方式可以大大提高插入速度。
            有時需要把一些數據內置到應用中,常用的有以下2種方式:其一直接拷貝制作好的SQLite數據庫文件,其二是使用系統提供的數據庫,然后把數據批量插入。我更傾向于使用第二種方式:使用系統創建的數據庫,然后批量插入數據。批量插入數據也有很多方法,那么那種方法更快呢,下面通過一個demo比較一下各個方法的插入速度。
            1、使用db.execSQL(sql)
            這里是把要插入的數據拼接成可執行的sql語句,然后調用db.execSQL(sql)方法執行插入。
          public void inertOrUpdateDateBatch(List<String> sqls) {
          SQLiteDatabase db = getWritableDatabase();
          db.beginTransaction();
          try {
          for (String sql : sqls) {
          db.execSQL(sql);
          }
          // 設置事務標志為成功,當結束事務時就會提交事務
          db.setTransactionSuccessful();
          } catch (Exception e) {
          e.printStackTrace();
          } finally {
          // 結束事務
          db.endTransaction();
          db.close();
          }
          }
            2、使用db.insert("table_name", null, contentValues)
            這里是把要插入的數據封裝到ContentValues類中,然后調用db.insert()方法執行插入。
          db.beginTransaction(); // 手動設置開始事務
          for (ContentValues v : list) {
          db.insert("bus_line_station", null, v);
          }
          db.setTransactionSuccessful(); // 設置事務處理成功,不設置會自動回滾不提交
          db.endTransaction(); // 處理完成
          db.close()
          3、使用InsertHelper類
            這個類在API 17中已經被廢棄了
          InsertHelper ih = new InsertHelper(db, "bus_line_station");
          db.beginTransaction();
          final int directColumnIndex = ih.getColumnIndex("direct");
          final int lineNameColumnIndex = ih.getColumnIndex("line_name");
          final int snoColumnIndex = ih.getColumnIndex("sno");
          final int stationNameColumnIndex = ih.getColumnIndex("station_name");
          try {
          for (Station s : busLines) {
          ih.prepareForInsert();
          ih.bind(directColumnIndex, s.direct);
          ih.bind(lineNameColumnIndex, s.lineName);
          ih.bind(snoColumnIndex, s.sno);
          ih.bind(stationNameColumnIndex, s.stationName);
          ih.execute();
          }
          db.setTransactionSuccessful();
          } finally {
          ih.close();
          db.endTransaction();
          db.close();
          }
            4、使用SQLiteStatement
            查看InsertHelper時,官方文檔提示改類已經廢棄,請使用SQLiteStatement
          String sql = "insert into bus_line_station(direct,line_name,sno,station_name) values(?,?,?,?)";
          SQLiteStatement stat = db.compileStatement(sql);
          db.beginTransaction();
          for (Station line : busLines) {
          stat.bindLong(1, line.direct);
          stat.bindString(2, line.lineName);
          stat.bindLong(3, line.sno);
          stat.bindString(4, line.stationName);
          stat.executeInsert();
          }
          db.setTransactionSuccessful();
          db.endTransaction();
          db.close();
            下圖是以上4中方法在批量插入1萬條數據消耗的時間
            可以發現第三種方法需要的時間最短,鑒于該類已經在API17中廢棄,所以第四種方法應該是最優的方法。

          posted on 2014-05-23 10:12 順其自然EVO 閱讀(13947) 評論(0)  編輯  收藏 所屬分類: android

          <2014年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 鹤山市| 清涧县| 阜新市| 桓台县| 南部县| 邢台县| 吉林省| 泽州县| 建始县| 威信县| 保亭| 宝坻区| 永吉县| 武强县| 班玛县| 贵南县| 灵武市| 八宿县| 阳东县| 增城市| 永安市| 伊宁县| 西充县| 南澳县| 丰镇市| 尼木县| 虎林市| 织金县| 宁晋县| 淮阳县| 洛川县| 东港市| 长葛市| 霍州市| 凤阳县| 即墨市| 蕲春县| 罗田县| 九龙县| 永昌县| 澄城县|