在rails 4 中 使用 CSV 组件来 把csv文件导入到数据库

  1. class HardWorker  
  2.   include Sidekiq::Worker  
  3.   require 'CSV'  
  4.   
  5.   def perform(file_path)  
  6.         csv_text = File.read(file_path)  
  7.         csv = CSV.parse(csv_text, :headers => true)  
  8.         csv.each do |row|  
  9.             gis_road_link = GisRoadLink.new   
  10.             gis_road_link.no = row["NO"]  
  11.             gis_road_link.fno = row["FNO"]  
  12.             gis_road_link.tno = row["TNO"]  
  13.             gis_road_link.from_name = row["FROM_NAME"]  
  14.             gis_road_link.to_name = row["TO_NAME"]  
  15.             gis_road_link.name = row["NAME"]  
  16.             gis_road_link.cds = row["CDS"]                      
  17.             gis_road_link.r_cds = row["R_CDS"]  
  18.             gis_road_link.len = row["LEN"]  
  19.             gis_road_link.ldkd = row["LDKD"]  
  20.             gis_road_link.jdcdkd = row["JDCDKD"]  
  21.             gis_road_link.fjdcdkd = row["FJDCDKD"]  
  22.             gis_road_link.rxdkd = row["RXDKD"]  
  23.             gis_road_link.jffgdkd = row["JFFGDKD"]  
  24.             gis_road_link.zyfgdkd = row["ZYFGDKD"]  
  25.             gis_road_link.hxkd = row["HXKD"]  
  26.             gis_road_link.f_t = row["F_T"]  
  27.             gis_road_link.t_f = row["T_F"]  
  28.             gis_road_link.dldj = row["DLDJ"]  
  29.             gis_road_link.jsfs = row["JSFS"]  
  30.             gis_road_link.fglx = row["FGLX"]  
  31.             gis_road_link.gjzyd = row["GJZYD"]  
  32.             gis_road_link.r_gjzyd = row["R_GJZYD"]  
  33.             gis_road_link.jtxtj = row["JTXTJ"]  
  34.             gis_road_link.r_jtxtj = row["R_JTXTJ"]  
  35.             gis_road_link.sjcs = row["SJCS"]  
  36.             gis_road_link.r_sjcs = row["R_SJCS"]  
  37.             gis_road_link.ldtxnl = row["LDTXNL"]  
  38.             gis_road_link.r_ldtxnl = row["R_LDTXNL"]  
  39.             gis_road_link.ldywhs = row["LDYWHS"]  
  40.             gis_road_link.r_ldywhs = row["R_LDYWHS"]  
  41.             gis_road_link.szxzq = row["SZXZQ"]  
  42.             gis_road_link.szjtxq = row["SZJTXQ"]  
  43.             gis_road_link.szjd = row["SZJD"]  
  44.             gis_road_link.szzt = row["SZZT"]  
  45.             gis_road_link.szfdtz = row["SZFDTZ"]  
  46.             gis_road_link.zhgxsj = row["ZHGXSJ"]  
  47.             gis_road_link.tpxx = row["TPXX"]  
  48.             gis_road_link.spxx = row["SPXX"]  
  49.   
  50.             gis_road_link.version = version  
  51.             if gis_road_link.save  
  52.                 puts ("No是:"+gis_road_link.no.to_s + '的数据导入成功')  
  53.             else  
  54.                 puts ("No是:"+gis_road_link.no.to_s + '的数据导入失败')  
  55.             end          
  56.         end  
  57. end  
posted @ 2015-09-09 13:51  PointNet  阅读(419)  评论(0编辑  收藏  举报