在我的Windows 7中,我已經安裝了Ruby 1.9.2,並安裝了以下的寶石:如何正確安裝Ruby的DBI gem,以便它可以與MySql交談?
*當地的寶石*
- DBD-mysql的(0.4.4)
- DBI(0.4.5)
- 棄用(2.0.1)
- httparty(0.8.1)
- 的rubygems更新(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>'
}
我在做什麼錯誤?
(很明顯,我沒有分享我的真實用戶名或密碼:) – 2012-01-12 20:45:22
作爲讓您的生活更輕鬆的技巧,我建議使用Ruby的ORM之一而不是DBI界面。 [ActiveRecord](http://ar.rubyonrails.org/)是Rails的一部分,可以單獨使用,而我最喜歡的[Sequel](http://sequel.rubyforge.org/)是獨立的。 ORM的好處是您可以獲得寫得很好的SQL代,並且可以輕鬆地從一個DBM移動到另一個DBM,而無需擔心更改SQL。 – 2012-01-12 21:17:47
爲什麼我的代碼在Linux上成功逐字運行,使用相同版本的dbi,dbd-mysql,並且已棄用。無論是否需要rubygems,它都可以工作。我似乎回想起安裝dbi時最初的心痛,我會四處探察,看看我的問題是否與您的問題有關。 – forforf 2012-01-12 21:24:59