MySQL and INFILE on Ruby

Hey Folks,

New here and to Ruby. Need some help trying to get a MySQL infile
command to work in Ruby:

dbh = Mysql.real_connect(“localhost”, “user”, “pass”, “db_test”)
dbh.query(“LOAD DATA LOCAL INFILE ‘C:\test.csv’ INTO TABLE
db_test.tbl_testing FIELDS TERMINATED BY ‘,’ ENCLOSED BY '”’ LINES
TERMINATED BY ‘\r\n’(data1,data2)
")
printf “%d rows were inserted\n”, dbh.affected_rows

However when I run the program it returns an error:
…LINES TERMINATED BY ‘\r\n’…
^
testcsv.rb:3: unterminated string meets end of file

Any ideas as to how to get around this issue… I’m assuming it has to
do with the \r\n … i’ve tried to use \r\n to no avail…

Thanks!

Amir…

[email protected] Amir wrote:

printf “%d rows were inserted\n”, dbh.affected_rows

Amir…

You do need to escape the \r\n but you also need to escape the " you
have in the string
so…

“LOAD DATA LOCAL INFILE ‘C:\test.csv’ INTO TABLE db_test.tbl_testing
FIELDS TERMINATED BY ‘,’
ENCLOSED BY '”’ LINES TERMINATED BY ‘\r\n’(data1,data2)"

(Note that would all be on one line, my email wraps it)

Should work, I use the same thing in my migrations with no problems.


Jim M., http://blog.wolfman.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs