• 在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  
  • 相关阅读:
    【转载】C#使用Split函数根据特定分隔符分割字符串
    【转载】 Asp.Net安全之防止脚本入
    【转载】C#使用as关键字将对象转换为指定类型
    【转载】C#使用is关键字检查对象是否与给定类型兼容
    【转载】C#将字符串中字母全部转换为大写或者小写
    【转载】使用Response.WriteFile输出文件以及图片
    【转载】常见面试题:C#中String和string的区别分析
    【转载】Asp.Net中Cookie对象的作用以及常见属性
    【转载】C#指定文件夹下面的所有内容复制到目标文件夹下面
    【转载】Asp.Net中应用程序的事件响应次序
  • 原文地址:https://www.cnblogs.com/goody9807/p/4794521.html
Copyright © 2020-2023  润新知