简体   繁体   English

使用Rubygems将CSV文件数据加载到Mysql中

[英]Loading CSV file Data into Mysql using Rubygems

Trying to insert the data into DB from a csv file using ruby programming. 尝试使用ruby编程将数据从csv文件插入DB。 I am fresher to scripting in ruby because of this facing issue. 由于这个问题,我对使用ruby编写脚本比较新鲜。

the data in CSV file is as follows: CSV文件中的数据如下:

12,prakash,nair,kerala,india
13,koteswarreddy,aula,andhra pradesh,india
14,kush,gambhir,punjab,india
15,james,arnold,NY,USA  

kindly, suggest me to load this data into the data base Employee using Ruby. 亲切,建议我使用Ruby将这些数据加载到Employee数据库中。

require 'csv'
load 'dbconnection.rb'
require 'activerecord'

class Employee<ActiveRecord::Base
end

in the Employee which i need to load the data. 员工 ,我需要加载数据。

You can proceed as follows 您可以按照以下步骤进行

require 'csv'    

csv_text = File.read(/path/to/file)
csv = CSV.parse(csv_text, :headers => false)
csv.each do |row|
  Employee.create!(row)
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM