zoukankan      html  css  js  c++  java
  • ruby批量插入数据,bulk_insert-----Gem包使用

    文档

    https://github.com/jamis/bulk_insert

    class Book < ActiveRecord::Base
    end
    
    book_attrs = ... # some array of hashes, for instance
    Book.bulk_insert do |worker|
      book_attrs.each do |attrs|
        worker.add(attrs)
      end
    end

    #向数据库批量插入数据
    "d_data_yyyymm_params"=>[{"station_id"=>1, "item_code"=>107, "data_cycle"=>300, "data_attr"=>"T", "data_time"=>201702160905, "data_value"=>148, "data_label"=>""},
    {"station_id"=>1, "item_code"=>108, "data_cycle"=>300, "data_attr"=>"T", "data_time"=>201702160905, "data_value"=>148, "data_label"=>""}]


    DDataYyyymm.bulk_insert(update_duplicates: true) do |worker|
    d_data_yyyymm_params.each do |fm|
    worker.add(fm)
    end
    end

    ##之前 以后不要出现这种插入数据
    d_data_yyyymm_params.each do |fm|
    d_data = DDataYyyymm.new
    d_data.station_id = fm.station_id
    d_data.item_code = fm.item_code


    d_data.save
    end

  • 相关阅读:
    [BUUCTF]PWN3——warmup_csaw_2016
    分页
    关系与外键约束
    连接查询
    自关联
    子查询
    视图
    事务
    索引
    绘图笔记
  • 原文地址:https://www.cnblogs.com/lmg-jie/p/9199529.html
Copyright © 2011-2022 走看看