简体   繁体   中英

Rails: CSV Import

i genereated an csv with columns id, task, description, created_at, updated_at

 create_table "tasks", :force => true do |t|
t.string   "task"
t.string   "description"
t.datetime "created_at",   :null => false
t.datetime "updated_at",   :null => false

I opened the sqlite Konsole

   rails db
   .separator ','
   .C:/User/jkl/Desktop/tasks.csv

I get error datatype mismatch. I dont fill in created_at and updated_at, but this is from rails to null => false.

How do you import?

I usually do it via a ruby script:

require 'csv'

def import
  CSV.foreach('path/to/file.csv', :col_sep => ';', :headers => true) do |row|
    Task.create!(row.to_hash)
  end
end

This works assuming your CSV headers match your column names, like so:

task;description
"Task 1";"Paint the fence"

Hope that helps!

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