如何正确安装Ruby的DBI gem以便它可以与MySql通信?

时间:2012-01-12 20:40:50

标签: ruby installation dbi

在我的Windows 7盒子上,我安装了Ruby 1.9.2,并安装了以下gem:

*本地宝石*

  • dbd-mysql(0.4.4)
  • dbi(0.4.5)
  • 已弃用(2.0.1)
  • httparty(0.8.1)
  • ruby​​gems-update(1.8.15)

(我是使用gem install完成的。)

我还写了以下简单的测试工具:

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

无论我如何指定连接字符串,我都会得到:

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

}

我做错了什么?

1 个答案:

答案 0 :(得分:1)

question中找到我的问题的答案。

在64位操作系统上运行Ruby时,需要将新版本的libMySql.dll复制到Ruby \ bin目录中。该dll的最佳来源是here