How to connect Ruby with MySQL (XAMPP)?

Dear All:
I need your help on following points.

  1. Can I connect Ruby with MySQL in XAMPP server?

  2. Could someone tell me that how to connect MySQL with Ruby? I have XAMPP server installed and want to connect with ruby.
    I am using Ruby1.8.7.
    I tried following code:
    require ‘rubygems’
    require ‘mysql’

    connect to the MySQL server

    dbh = Mysql.connect(“localhost”, “root”, “root”, “status_publish”)
    puts dbh

    get server version string and display it

    puts "Server version: " + dbh.get_server_info
    rescue Mysql::Error => e
    puts “Error code: #{e.errno}”
    puts “Error message: #{e.error}”
    puts “Error SQLSTATE: #{e.sqlstate}” if e.respond_to?(“sqlstate”)

    disconnect from server

    dbh.close if dbh
    OUTPUT: C:/xampp/htdocs/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require’: no such file to load – mysql (LoadError)

    from C:/xampp/htdocs/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require’
    from ruby_dbconnect.rb:4

Please help.

Thanks in advanced.

I got the solution. I just ran a command: gem install mysql and the error went away.

Now I am strugling with- how to select data from the mysql table. I tried so many ways but no luck.

Could somebody add focus on this?

Thanks in advance.



I suggest you look at the Rails Guides, start with Getting Started,
obviously, and work through some Rails tutorials is
good and free to use online. Make sure you use Rails 3 and that any
tutorials you try are for Rails 3


You're probably going to get more takers on the ruby-talk list, rather than rails-talk. We're all very busy with our integrated framework, rather than trying to build a connection to a database with stone knives and bear skins. (kidding about that!)

You've got a database handle (dbh). Before you close it in your script, use it to issue a SQL command to the server, like this:

res = dbh.query("SELECT name, category FROM animal")
while row = res.fetch_hash do
  printf "%s, %s\n", row["name"], row["category"]

This is all from here: