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’

begin
# 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”)
ensure
# disconnect from server
dbh.close if dbh
end

*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:inrequire’
from ruby_dbconnect.rb:4

Please help.

Thanks in advanced.


Thanks and Regards,
Amit

Hi,

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.

Thanks,
Amit

On Wed, Aug 24, 2011 at 8:44 PM, Amit B. [email protected]
wrote:

I am using Ruby1.8.7.
# get server version string and display it
*OUTPUT:
*C:/xampp/htdocs/Ruby187/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in

Thanks and Regards,
Amit


Thanks and Regards,
Amit

On 24 August 2011 16:37, Amit B. [email protected] wrote:

Hi,

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.

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

Colin

On Aug 24, 2011, at 11:37 AM, Amit B. wrote:

Hi,

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?

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”]
end

This is all from here:
http://www.kitebird.com/articles/ruby-mysql.html#TOC_7

Walter