Angle Tom
Angle Tom

Reputation: 1130

I have an error when I import the data into postgresql from csv file with `sequel`

I am trying to import the data into postgresql from csv files as follows.

csv = CSV.open(path, { col_sep: ',', headers: :first_row })

MatchDB.db.copy_into(:property,
  data: csv,
  format: :csv
)

But I got an error like this.

TypeError: wrong argument type CSV::Row (expected String)

Anyone knows how can I fix that?

Upvotes: 1

Views: 201

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

As you might see in Sequel’s tests, the Database#copy_into method expects a string with columns separated by commas and rows separated by line separator \n.

You are trying to pass a ruby internal CSV representation, which is [generally speaking kinda] an array of arrays. I am not a guru in ruby CSV, but to make a proper string out of it, one might use:

csv.map { |row| row.join ',' }.join "\n"

The summing up:

csv = CSV.open(path, { col_sep: ',', headers: :first_row })

MatchDB.db.copy_into(:property,
  data: csv.map { |row| row.join ',' }.join "\n",
  format: :csv
)

Upvotes: 1

Related Questions