Ruby学习札记(4)-安装dbi(解决deprecate问题)

Ruby学习札记(4)-安装dbi(解决deprecate问题)

 

dbi,即database interface,是Ruby访问数据库的模块,提供访问多种数据库的接口。

Architecture of a DBI Application,如图

 

一般方法是:下载dbitar.gzzip格式),CMD下切换到解压的目录,使用以下命令:

ruby setup.rb config (或ruby setup.rb config --with=dbi,dbd_mysql

ruby setup.rb setup

ruby setup.rb install

我试过—with选项,结果提示没有该参数。只好去掉—with选项。完成以上3个命令后在irb中,输入require ‘dbi’,结果报错。

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 `<t op (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>

    根据错误信息,大致知道在dbi.rb文件48行中,找不到deprecated文件。Google一下,知道还要一个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.r b:57:in `<class:Date>' from C:/MyProgramFiles/Ruby192/lib/ruby/site_ruby/1.9.1/dbi/utils/date.r b:7:in `<module:DBI>' from C:/MyProgramFiles/Ruby192/lib/ruby/site_ruby/1.9.1/dbi/utils/date.r b: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 `<t op (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>

我推荐使用gem方式安装dbi,我用的是离线方式。CMD下切换到下载gem所在的目录,输入gem install ./dbi-0.4.3.gem –localruby会检查依赖关系,直接安装不上dbi,提示需要deprecated

C:/Users/wesley_chen>gem install ./dbi-0.4.3.gem --local ERROR: Error installing ./dbi-0.4.3.gem: dbi requires deprecated (>= 2.0.0, runtime)

这个比前面的3个命令要好得多,免得安装上又用不起的问题。这里当然是先安装deprecatedgem(也是deprecated-2.0.1.gem才管用,不能使用deprecated-3.0.0.gem!),然后再安装dbi-0.4.3.gem。同样require 'dbi'会返回一个true值,表示dbi可以找到。

 

安装dbi后,只是有了数据库访问抽象层,没有具体的数据库驱动层,访问不了数据库,因此需要安装具体dbd,这里使用dbd-mysql。同样,可以在线或离线安装,在线则输入命令:gem install dbd-mysql(具体安装gem可以参考我的另一篇文章)

用以下代码测试

require 'dbi' begin dbh = DBI.connect("dbi:Mysql:test:localhost", "root", "123") row = dbh.select_one("SELECT VERSION()") puts "MySQL Version is: #{row[0]}" rescue DBI::DatabaseError => e puts "-----Error-----" puts "Error number: #{e.err}" puts "Error info: #{e.errstr}" ensure dbh.disconnect if dbh end

 

如果输出MySQL Version is: 5.1.39-community,说明dbi可以正常工作。

 

 

参考资料:

http://blog.csdn.net/ruby_cn/archive/2004/11/09/174293.aspx

http://dennis-zane.iteye.com/blog/69781

http://www.tutorialspoint.com/ruby/ruby_database_access.htm

你可能感兴趣的:(数据库,mysql,application,database,Ruby,deprecated)