How do I get Ruby's DBI gem properly installed

2019-08-29 09:51发布

问题:

On my Windows 7 box, I've installed Ruby 1.9.2, and installed the following gems:

* LOCAL GEMS *

  • dbd-mysql (0.4.4)
  • dbi (0.4.5)
  • deprecated (2.0.1)
  • httparty (0.8.1)
  • rubygems-update (1.8.15)

(I did this using gem install).

I have also written the following simple test harness:

require 'rubygems'
require 'dbi'

begin
    dbh = DBI.connect("DBI:Mysql:test", "username", "pwd")

    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

Regardless of how I specify the connection string, I keep getting:

C:\Code\Concordance.SE>ruby test_sql.rb
C:/Tools/Ruby192/lib/ruby/gems/1.9.1/gems/dbi-0.4.5/lib/dbi.rb:300:in `block in
load_driver': Unable to load driver 'Mysql' (underlying error: uninitialized con
stant DBI::DBD::Mysql) (DBI::InterfaceError)
        from C:/Tools/Ruby192/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'

        from C:/Tools/Ruby192/lib/ruby/gems/1.9.1/gems/dbi-0.4.5/lib/dbi.rb:242:
in `load_driver'
        from C:/Tools/Ruby192/lib/ruby/gems/1.9.1/gems/dbi-0.4.5/lib/dbi.rb:160:
in `_get_full_driver'
        from C:/Tools/Ruby192/lib/ruby/gems/1.9.1/gems/dbi-0.4.5/lib/dbi.rb:145:
in `connect'
        from test_sql.rb:4:in `<main>'

}

What am I doing wrong?

回答1:

Found the answer to my problem in this question.

When you are running Ruby on a 64 bit operating system, you need to copy a new version of libMySql.dll into the Ruby\bin directory. The best source for that dll is here.