DataMapper 模型的 FasterCSV 导入器 - 未插入行

发布于 2024-09-25 18:51:17 字数 1257 浏览 0 评论 0原文

我有一个模型(称为 Test):

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'}) do |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
  )

row_data.save

end

当我运行导入程序时,没有出现任何错误。

我是否缺少一些明显的内容?并且字段名称与我的文件中的标题相同。

I have a model (called Test):

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

I am importing data from a tab delimited file, using FasterCSV,

FasterCSV.foreach("test.txt", {:headers => true, :quote_char=>'"', :col_sep =>'/t'}) do |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
  )

row_data.save

end

No errors appear, when I run the importer. Nothing appears inserted in SQLite table.

Am i missing something obvious? (The table exists within the target database, and the field names are the same as the headers from my file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疯到世界奔溃 2024-10-02 18:51:17

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

有两个问题,我猜您

  • 打算使用的分隔符是“\t”而不是“/t”
  • you'不使用 row_data 来填充 datamapper 对象

这应该工作得更好:

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

Update 2014/11/19: FasterCSV has been removed. Ruby standard library CSV should now be used intead. Just replace all occurrences of FasterCSV with CSV

There's two problem i guess

  • the delimiter you intended to use was rather "\t" than '/t'
  • you're not using the row_data to populate the datamapper object

This should work better:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文