How do I get Ruby's DBI gem properly installed so that it can talk to MySql?

三世轮回 提交于 2019-12-25 02:22:22

问题


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.



来源:https://stackoverflow.com/questions/8841974/how-do-i-get-rubys-dbi-gem-properly-installed-so-that-it-can-talk-to-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!