Ruby connection to MySql

I’m going thru the “PickAxe” book but can find only limited information
in it regarding how Ruby connects to and uses MySql…

Is there any documentation available in regard to this?

Thanks,
Pat

On 9/21/06, Patrick L. [email protected] wrote:

I’m going thru the “PickAxe” book but can find only limited information
in it regarding how Ruby connects to and uses MySql…

Is there any documentation available in regard to this?

Here is the website of the (well, ‘a’…) Ruby library for MySQL:
http://www.tmtm.org/en/mysql/ruby/

This is a relatively low-level way to work with a database.
ActiveRecord, Og, rBATIS, etc, are higher-level ORM libraries that
make life easier.

At 3:13 AM +0900 9/22/06, Patrick L. wrote:

I’m going thru the “PickAxe” book but can find only
limited information in it regarding how Ruby connects
to and uses MySql…

Is there any documentation available in regard to this?

As far as I know, Ruby itself has no language support for
MySQL in particular or databases in general. However, a
number of libraries have been developed to allow Ruby to
interact with these facilities.

The most popular interface, Active Record, is part of the
“Ruby on Rails” effort. Several books are available on
this topic, including:

Agile Web D. with Rails
Rails Recipes
Ruby for Rails
Ruby on Rails: Up and Running

General coverage of database usage in Ruby is given in:

Enterprise Integration with Ruby

This book also covers LDAP, XML, and distributed apps. I
like the book’s approach, which starts with low-level
solutions and works its way up to more abstract interfaces.

This allows the reader to appreciate the problems that the
higher-level solutions are dealing with (and, if it becomes
necessary to revert to a lower-level approach, what options
are available).

-r

http://www.cfcl.com/rdm Rich M.
http://www.cfcl.com/rdm/resume [email protected]
http://www.cfcl.com/rdm/weblog +1 650-873-7841

Technical editing and writing, programming, and web development

John G. wrote:

On 9/21/06, Patrick L. [email protected] wrote:

I’m going thru the “PickAxe” book but can find only limited information
in it regarding how Ruby connects to and uses MySql…

Is there any documentation available in regard to this?

Hi Pat,

Here’s what I’ve been able to gather on the matter:
http://www.simisen.com/jmg/notes/ruby/db.html

—John

Hi John,
Thanks for the link…I’ll work it and let you know how I make out…
I hope I can return the favor someday.
Ciao,
Pat

On 9/21/06, Patrick L. [email protected] wrote:

I’m going thru the “PickAxe” book but can find only limited information
in it regarding how Ruby connects to and uses MySql…

Is there any documentation available in regard to this?

Hi Pat,

Here’s what I’ve been able to gather on the matter:
http://www.simisen.com/jmg/notes/ruby/db.html

—John

Patrick L. wrote:

Here’s what I’ve been able to gather on the matter:
I should have noted that I’m trying to get Ruby to talk to MySql on a
Windows XP/Pro box.

I was able to get Rails working with MySql and I can’t understand why
there is no support for MySql and other DBMSs in Ruby. The ‘Pick Axe’
makes a reference to ‘dbi’ on page 157, but offers no help in installing
it…bummer.

So, for my Ruby app, I’ll simply use a flat file and abandon my effort
to interface with MySql. What a shame.

Um… If you’ve got Rails working with MySQL, you’ve got Ruby working
with MySQL. You should just be able to require ‘mysql’ from an
arbitrary script and have it work, when Rails is installed.

On 9/28/06, Patrick L. [email protected] 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.

Patrick L. wrote:

John G. wrote:

On 9/21/06, Patrick L. [email protected] wrote:

I’m going thru the “PickAxe” book but can find only limited information
in it regarding how Ruby connects to and uses MySql…

Is there any documentation available in regard to this?

Hi Pat,

Here’s what I’ve been able to gather on the matter:
http://www.simisen.com/jmg/notes/ruby/db.html

—John

Hi John,
Thanks for the link…I’ll work it and let you know how I make out…
I hope I can return the favor someday.
Ciao,
Pat

Good morning,
I should have noted that I’m trying to get Ruby to talk to MySql on a
Windows XP/Pro box.

I was able to get Rails working with MySql and I can’t understand why
there is no support for MySql and other DBMSs in Ruby. The ‘Pick Axe’
makes a reference to ‘dbi’ on page 157, but offers no help in installing
it…bummer.

So, for my Ruby app, I’ll simply use a flat file and abandon my effort
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…

Wilson B. wrote:

On 9/28/06, Patrick L. [email protected] 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

On 9/28/06, Patrick L. [email protected] wrote:

My next app will be put up with Rails…wish me luck…

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.

Wilson B. wrote:

On 9/28/06, Patrick L. [email protected] wrote:

My next app will be put up with Rails…wish me luck…

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):
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.

Worked beautifully! THX. I had been working with MySql previously so I
had to check on the ‘put_your_database_name_here’ and thankfully I save
the password.
(I’m not quite sure what would have to be done if I had lost the
password).

I’ve worked extensively with DB2 and SQL Server 2000, so working at a
low level is no big woops for me, actually, I enjoy working at the this
level…

If you attend any of the Ruby U. Group Meetings, let me know, I’d like
to thank you in person…

THX,
Pat