简体   繁体   中英

Loading CSV file Data into Mysql using Rubygems

Trying to insert the data into DB from a csv file using ruby programming. I am fresher to scripting in ruby because of this facing issue.

the data in CSV file is as follows:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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