Topic: Question

I have a question : how would you write the results of a query on a mysql database to a text file using rail ?

Thanks

Re: Question

I have continued my researches and it seems that the command system() would do the job.

Last edited by gill_tasse (2007-05-13 05:30:32)

Re: Question

You can perform any SQL query directly with the "execute" method on a connection. Like this:

ActiveRecord::Base.connection.execute("SELECT ...")

If you want to export it to a file, you can use the mysql command INTO OUTFILE.

SELECT * FROM items INTO OUTFILE 'path/to/file.txt'

Railscasts - Free Ruby on Rails Screencasts

Re: Question

gill_tasse wrote:

I have continued my researches and it seems that the command system() would do the job.

A better/worse equivalent to system() is to use backquotes (`) to wrap the command.

(backquotes are the ones found on the top left of the keyboard)

Therefore,

puts `date`

is more or less equivalent to
puts system("date")

However, system() returns true after running the command, so something like:
if `date`
  puts "blah"
end

if system("date")
  puts "blah again"
end


Will output:

blah
Sun May 13 12:50:42 MDT 2007
blah again

Last edited by Firanide (2007-05-13 14:50:50)

Re: Question

Thank you Sirs !