Ruby connection to MySql

P

Patrick Lynch

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
 
W

Wilson Bilkovich

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.
 
R

Rich Morin

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 Development 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 Morin
http://www.cfcl.com/rdm/resume (e-mail address removed)
http://www.cfcl.com/rdm/weblog +1 650-873-7841

Technical editing and writing, programming, and web development
 
P

Patrick Lynch

Patrick said:
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...
 
A

Alex Young

Patrick said:
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.

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.
 
W

Wilson Bilkovich

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

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.
 
P

Patrick Lynch

Wilson said:
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
 
W

Wilson Bilkovich

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.
 
P

Patrick Lynch

Wilson said:
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.

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 User Group Meetings, let me know, I'd like
to thank you in person...

THX,
Pat
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top