Getting Data From MySQL Using Ruby.

I wanted a fast way to get data from mysql using ruby. after i installed the mysql gem it was really easy. This program gets all rows, but only displays the id,name and zip.

require "mysql"

dbh = Mysql.real_connect("localhost", "root", "password", "scottwork")
res = dbh.query("SELECT name,address,city,state,zip,email FROM contacts")

while row = res.fetch_row do
    id = row[0]
    name = row[1]
    address = row[2]
    city = row[3]
    state = row[4]
    zip = row[5]
    email = row[6]
    puts id+','+name+','+zip
end

res.free
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • LinkedIn
  • RSS
  • StumbleUpon
  • Twitter

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)