« Return to Thread: Using DBI and MySQL gems

Re: Using DBI and MySQL gems

by Erik Hollensbe :: Rate this Message:

Reply to Author | View in Thread

Owein Herrmann wrote:
> Quick question:
>
> I thought using rubygems that to use the dbi and mysql to access a MySQL
> database from Ruby I would only have to do the following:
>
> gem 'mysql'
> gem 'dbi'
>
> and then I could do whatever, eg:

I just wanted to address this because... it's been driving me nuts all
day. :)

Here's a script that works great on my dev system, stock libs, gem env,
DBI and DBD::Mysql latest release gems:

~% gem env
[1:19]
RubyGems Environment:
  - VERSION: 0.9.4 (0.9.4)
  - INSTALLATION DIRECTORY: /var/lib/gems/1.8
  - GEM PATH:
     - /var/lib/gems/1.8
  - REMOTE SOURCES:
     - http://gems.rubyforge.org

---

require "rubygems"
require "dbi"

begin
    # connect to the MySQL server
    dbh = DBI.connect("DBI:Mysql:rubytest:localhost", "root")
    # get server version string and display it
    row = dbh.select_one("SELECT VERSION()")
    puts "Server version: " + row[0]
rescue DBI::DatabaseError => e
    puts "An error occurred"
    puts "Error code: #{e.err}"
    puts "Error message: #{e.errstr}"
ensure
    # disconnect from server
    dbh.disconnect if dbh
end

---

If this breaks on your system, I'd be looking elsewhere (of ruby,
rubygems, and dbi... likely in the direction of your package
maintainers) to solve your problem.

-Erik
--
Posted via http://www.ruby-forum.com/.

 « Return to Thread: Using DBI and MySQL gems