如何在数据库中添加 csv 日期
How add date of csv on database
我有包含以下内容的文件:
purchaser name item description item price purchase count merchant address merchant name
João Silva R off R of food 10.0 2 987 Fake St Bob's Pizza
Amy Pond R of awesome for R 10.0 5 456 Unreal Rd Tom's Awesome Shop
Marty McFly R Sneakers for R 5.0 1 123 Fake St Sneaker Store Emporium
Snake Plissken R Sneakers for R 5.0 4 123 Fake St Sneaker Store Emporium
我是这样创建数据库的:
我有以下代码获取数据:
require 'csv'
CSV.foreach('myfile.txt', col_sep: "\t", headers: true).map do |row|
row.to_h
end
我挤压它时得到以下哈希值:
[{"purchaser name"=>"João Silva", "item description"=>"R off R of food", "item price"=>"10.0", "purchase count"=>"2", "merchant address"=>"987 Fake St", "merchant name"=>"Bob's Pizza"}, {"purchaser name"=>"Amy Pond", "item description"=>"R of awesome for R", "item price"=>"10.0", "purchase count"=>"5", "merchant address"=>"456 Unreal Rd", "merchant name"=>"Tom's Awesome Shop"}, {"purchaser name"=>"Marty McFly", "item description"=>"R Sneakers for R", "item price"=>"5.0", "purchase count"=>"1", "merchant address"=>"123 Fake St", "merchant name"=>"Sneaker Store Emporium"}, {"purchaser name"=>"Snake Plissken", "item description"=>"R Sneakers for R", "item price"=>"5.0", "purchase count"=>"4", "merchant address"=>"123 Fake St", "merchant name"=>"Sneaker Store Emporium"}]
如何将此散列的每一项放入数据库中适当的 table?
CSV.foreach('myfile.txt', col_sep: "\t", headers: true).map do |row|
records = row.to_h
# you can set the records as variables:
purchaser = records["purchaser name"]
item_description = records["item description"]
item_price = records["item price"]
purchase_count = records["purchase count"]
merchant_address = records["merchant_address"]
merchant_name = records["merchant name"]
# now that you have the content as variables do you can whatever you want with them
# Merchant.where(name: merchant_name), etc...
end
我有包含以下内容的文件:
purchaser name item description item price purchase count merchant address merchant name
João Silva R off R of food 10.0 2 987 Fake St Bob's Pizza
Amy Pond R of awesome for R 10.0 5 456 Unreal Rd Tom's Awesome Shop
Marty McFly R Sneakers for R 5.0 1 123 Fake St Sneaker Store Emporium
Snake Plissken R Sneakers for R 5.0 4 123 Fake St Sneaker Store Emporium
我是这样创建数据库的:
我有以下代码获取数据:
require 'csv'
CSV.foreach('myfile.txt', col_sep: "\t", headers: true).map do |row|
row.to_h
end
我挤压它时得到以下哈希值:
[{"purchaser name"=>"João Silva", "item description"=>"R off R of food", "item price"=>"10.0", "purchase count"=>"2", "merchant address"=>"987 Fake St", "merchant name"=>"Bob's Pizza"}, {"purchaser name"=>"Amy Pond", "item description"=>"R of awesome for R", "item price"=>"10.0", "purchase count"=>"5", "merchant address"=>"456 Unreal Rd", "merchant name"=>"Tom's Awesome Shop"}, {"purchaser name"=>"Marty McFly", "item description"=>"R Sneakers for R", "item price"=>"5.0", "purchase count"=>"1", "merchant address"=>"123 Fake St", "merchant name"=>"Sneaker Store Emporium"}, {"purchaser name"=>"Snake Plissken", "item description"=>"R Sneakers for R", "item price"=>"5.0", "purchase count"=>"4", "merchant address"=>"123 Fake St", "merchant name"=>"Sneaker Store Emporium"}]
如何将此散列的每一项放入数据库中适当的 table?
CSV.foreach('myfile.txt', col_sep: "\t", headers: true).map do |row|
records = row.to_h
# you can set the records as variables:
purchaser = records["purchaser name"]
item_description = records["item description"]
item_price = records["item price"]
purchase_count = records["purchase count"]
merchant_address = records["merchant_address"]
merchant_name = records["merchant name"]
# now that you have the content as variables do you can whatever you want with them
# Merchant.where(name: merchant_name), etc...
end