views:

54

answers:

1

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.

+1  A: 

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
hellvinz
Thanks, I've used your version, with the correct tab delimiter and using the correct way to find the items from the row_data. Though still not getting anything importing. Anyway to get FasterCSV to produce logs, or would it error if data was wrong?
Neil
yes sure, you can add a "puts row_data.inspect" right after the foreach line in order to trace what the row_data contains. (by the way the "\t" means that your fields are tab separated, is it ok?)
hellvinz
Correct it is tab delimited, an example row output from FasterCSV confirms it is assigning the right values to each column when reading. Just the save method seems to not be hitting into DataMapper.
Neil
since all fields seems to be required, it may fail because the instance is not valid. Are you sure that they are all filled (check the valid? method)
hellvinz
OK I changed the model to allow nulls, and just import the title. SQLite table reflects just an ID and Title. Now receiving an error: 'tests.description may not be NULL (DataObjects::IntegrityError)'
Neil
there should be a supplier field in the csv empty then
hellvinz
Are you saying FasterCSV can't cope if the field it picks up is null?
Neil
oh my bad i haven't read enough your answer. So you are saying that you have changed your model (removed all required => true for all fields but title). Then if you have this error, it must come from sqlite itself, have you migrated the schema to allow null values for descriptions (http://datamapper.org/getting-started#set_up_your_database_tables)
hellvinz
Sorted it, my paths to the DB were wrong. But all working now.
Neil