原本的DBI模組的安裝教程:
1.安裝
首先,我是使用mysql進行測試的,你的機器上需要安裝mysql資料庫。
然後執行:
gem install mysql
到rubyforge下載ruby-DBI,解壓後cd到目錄運行如下命令:
ruby setup.rb config --with=dbi,dbd_mysql
ruby setup.rb setup
ruby setup.rb install
完整的setup命令參數參考DBI的doc
2.完整例子
DBI是一類似於ODBC的開發式的統一的資料庫編程介面,結構層次上可以分為兩層:
1.Database Interface——資料庫介面層,與資料庫無關,提供與資料庫無關的標準介面
2.Database Driver——資料庫驅動,與資料庫相關
DBI也是很簡單易用的,一個完整的使用例子,對於初學者可能有點協助:
require 'dbi'begin #串連資料庫 dbh=DBI.connect("DBI:Mysql:dbi_test:localhost","root","") dbh.columns("simple").each do |h| p h end #示範3種交易處理方式 #手動commit dbh["AutoCommit"]=false 1.upto(10) do |i| sql = "insert into simple (name, author) VALUES (?, ?)" dbh.do(sql, "Song #{i}", "#{i}") end dbh.commit #使用transaction方法 dbh.transaction do |dbh| 1.upto(10) do |i| sql = "insert into simple (name, author) VALUES (?, ?)" dbh.do(sql, "Song #{i}", "#{i}") end end #使用SQL語句 dbh.do("SET AUTOCOMMIT=0") dbh.do("BEGIN") dbh["AutoCommit"]=false dbh.do("UPDATE simple set name='test' where id='1'") dbh.do("COMMIT") #查詢 sth=dbh.execute("select count(id) from simple") puts "bookCount:#{sth.fetch[0]}" sth.finish begin sth=dbh.prepare("select * from simple") sth.execute while row=sth.fetch do p row end sth.finish rescue end #上面這段查詢可以改寫為: #dbh.select_all("select * from simple") do |row| # p row #end #使用工具類輸出xml格式結果集以及測量查詢時間 sql="select * from simple" mesuretime=DBI::Utils::measure do sth=dbh.execute(sql) end puts "SQL:#{sql}" puts "Time:#{mesuretime}" rows=sth.fetch_all col_names=sth.column_names sth.finish puts DBI::Utils::XMLFormatter.table(rows) dbh.do("delete from simple")rescue DBI::DatabaseError=>e puts "error code:#{e.err}" puts "Error message:#{e.errstr}"ensure dbh.disconnect if dbhend
出現問題:
但是每次我執行第一步的時候就會出現 config: unknown option --with=dbi,dbd_mysql Try 'ruby setup.rb --help' for detailed usage.
原因分析:
這是由於dbi版本問題造成的。
因此新版本的安裝方式如下:
一般方法是:下載dbi(tar.gz或zip格式),CMD下切換到解壓的目錄,使用以下命令:
ruby setup.rb config (或ruby setup.rb config --with=dbi,dbd_mysql)
ruby setup.rb setup
ruby setup.rb install
報錯:
irb(main):001:0> require 'dbi'LoadError: no such file to load -- deprecated from <internal:lib/rubygems/custom_require>:29:in `require' from <internal:lib/rubygems/custom_require>:29:in `require' from C:/MyProgramFiles/Ruby192/lib/ruby/site_ruby/1.9.1/dbi.rb:48:in `<top (required)>' from <internal:lib/rubygems/custom_require>:29:in `require' from <internal:lib/rubygems/custom_require>:29:in `require' from (irb):1 from C:/MyProgramFiles/Ruby192/bin/irb:12:in `<main>'irb(main):002:0>
這個問題比較簡單,只要安裝一個deprecated包,於是我下載了deprecated-3.0.0.gem,安裝之後還是報錯。
irb(main):001:0> require 'dbi'NoMethodError: undefined method `deprecate' for DBI::Date:Class from C:/MyProgramFiles/Ruby192/lib/ruby/site_ruby/1.9.1/dbi/utils/date.rb:57:in `<class:Date>' from C:/MyProgramFiles/Ruby192/lib/ruby/site_ruby/1.9.1/dbi/utils/date.rb:7:in `<module:DBI>' from C:/MyProgramFiles/Ruby192/lib/ruby/site_ruby/1.9.1/dbi/utils/date.rb:1:in `<top (required)>' from <internal:lib/rubygems/custom_require>:29:in `require' from <internal:lib/rubygems/custom_require>:29:in `require' from C:/MyProgramFiles/Ruby192/lib/ruby/site_ruby/1.9.1/dbi/utils.rb:56:in `<top (required)>' from <internal:lib/rubygems/custom_require>:29:in `require' from <internal:lib/rubygems/custom_require>:29:in `require' from C:/MyProgramFiles/Ruby192/lib/ruby/site_ruby/1.9.1/dbi.rb:50:in `<top (required)>' from <internal:lib/rubygems/custom_require>:29:in `require' from <internal:lib/rubygems/custom_require>:29:in `require' from (irb):1 from C:/MyProgramFiles/Ruby192/bin/irb:12:in `<main>'irb(main):002:0>
再次Google,居然是deprecated-3.0.0.gem太新了!換成deprecated-2.0.1.gem就可以了。
irb(main):001:0> require 'dbi' => true irb(main):002:0>
這個問題還是比較容易解決的,不過要是出現如下問題就不好解決了:
<internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- parsedate (LoadError)from <internal:lib/rubygems/custom_require>:29:in `require'from c:/Program Files (x86)/RailsInstaller/Ruby1.9.2/lib/ruby/site_ruby/1.9.1/dbi/sql.rb:9:in `<module:DBI>'from c:/Program Files (x86)/RailsInstaller/Ruby1.9.2/lib/ruby/site_ruby/1.9.1/dbi/sql.rb:7:in `<top (required)>'from <internal:lib/rubygems/custom_require>:29:in `require'from <internal:lib/rubygems/custom_require>:29:in `require'from c:/Program Files (x86)/RailsInstaller/Ruby1.9.2/lib/ruby/site_ruby/1.9.1/dbi.rb:37:in `<top (required)>'from <internal:lib/rubygems/custom_require>:29:in `require'from <internal:lib/rubygems/custom_require>:29:in `require'from E:/Program/Eclipse/RubyTest/dbi.rb:1:in `<main>'
經過查詢之後,才發現原來parsedate在ruby1.9.2中已經被廢除了,現在使用的是date。因此這個版本問題真的是沒有辦法進行解決啦。我現在正在考慮要不要把ruby降級來解決這個問題。嘗試中,後面如果解決了會進行更新。