From: Wilson Bilkovich Date: 2006-09-29T05:29:16+09:00 Subject: Re: Ruby connection to MySql On 9/28/06, Patrick Lynch wrote: > Wilson Bilkovich wrote: > > On 9/28/06, Patrick Lynch wrote: > >> >> > >> > >> to interface with MySql. What a shame. > >> > >> In addition, classes mentioned in "Mastering Perl/Tk' (such as > >> CamvasPlot) cannot, AFAIK, be used with Ruby...if anyone knows how to > >> make these Perl classes known to Ruby, please let me know... > >> > >> My next app will be put up with Rails...wish me luck... > >> > > > > Ruby has tons of database support. I routinely use MySQL, Oracle, > > PostgreSQL, and SQLite from Ruby code on my Windows XP box. > > You're just giving up too quickly. > > Wilson, > Thanks for the 'heads up'...can you please give me some direction to get > Ruby talking to MySql? > Pat > First, either: gem install mysql --remote (pick the Win32 version when/if given a choice.) ..or install it by hand from here (needs a compiler): http://www.tmtm.org/en/mysql/ruby/ Once you have that, try running this code. require 'rubygems' require 'mysql' HOST = 'localhost' USER = 'root' PASSWORD = nil DATABASE = 'put_your_database_name_here' connection = Mysql.real_connect(HOST, USER, PASSWORD, DATABASE) connection.query "show tables" do |result_set| result_set.each {|row| puts row} end This is painfully low-level, and not really how anyone should be writing database code in Ruby. However, if it prints out a list of the tables in your database, you know it's working. In real life, you should use something like DBI, ActiveRecord, or Og. That way you aren't dealing with the nitty-gritty connection details all the time, and it is easier to switch databases later. DBI is also fairly low-level, but at least it is the same across all database platforms.