gpt4 book ai didi

ruby - FasterCSV 导入器到 DataMapper 模型 - 没有插入行

转载 作者:太空宇宙 更新时间:2023-11-03 16:10:56 24 4
gpt4 key购买 nike

我有一个模型(称为测试):

property :id,           Serial  
property :title, String, :length => 255, :required => true
property :description, String, :length => 255, :required => true
property :brand, String, :length => 255, :required => true
property :link, String, :length => 255, :required => true
property :image_link, String, :length => 255, :required => true
property :price, String, :length => 255, :required => true
property :condition, String, :length => 255, :required => true
property :product_type, String, :length => 255, :required => true

我正在使用 FasterCSV 从制表符分隔文件导入数据,

FasterCSV.foreach("test.txt", {:headers => true, :quote_char=>'"', :col_sep =>'/t'}) 做 |row_data|

 row_data = Test.first_or_new(
'title' => :title,
'description' => :supplier,
'brand' => :brand,
'link' => :link,
'image_link' => :image_link,
'price' => :price,
'condition' => :condition,
'product_type' => :product_type
)

行数据.保存

结束

当我运行导入程序时,没有出现任何错误。 SQLite 表中没有插入任何内容。

我是否遗漏了一些明显的东西? (该表存在于目标数据库中,字段名称与我文件中的标题相同。

最佳答案

2014 年 11 月 19 日更新:FasterCSV 已被删除。现在应该使用 Ruby 标准库 CSV。只需将所有出现的 FasterCSV 替换为 CSV

我猜有两个问题

  • 您打算使用的分隔符是“\t”而不是“/t”
  • 您没有使用 row_data 来填充数据映射器对象

这应该会更好:

FasterCSV.foreach("test.txt", {:headers => true, :quote_char=>'"', :col_sep =>"\t"}) do |row_data|

new_record = Test.first_or_new(
'title' => row_data['title'],
'description' => row_data['supplier'],
'brand' => row_data['brand'],
'link' => row_data['link'],
'image_link' => row_data['image_link'],
'price' => row_data['price'],
'condition' => row_data['condition'],
'product_type' => row_data['product_type']
)
new_record.save
end

关于ruby - FasterCSV 导入器到 DataMapper 模型 - 没有插入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3808026/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com