• 使用Ruby DBI模块


    原本的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 dbh
    end   
    出现问题:
    但是每次我执行第一步的时候就会出现 config: unknown option --with=dbi,dbd_mysql Try 'ruby setup.rb --help' for detailed usage. 
    原因分析:
    这是由于dbi版本问题造成的。
    因此新版本的安装方式如下:

    一般方法是:下载dbitar.gzzip格式),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 `<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>

    这个问题比较简单,只要安装一个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> 


    这个问题还是比较容易解决的,不过要是出现如下问题就不好解决了:

    <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降级来解决这个问题。尝试中,后面如果解决了会进行更新。

  • 相关阅读:
    在django中,执行原始sql语句
    Django的select_related 和 prefetch_related 函数优化查询
    Django -- settings 详解
    Django-models的字段类型
    ORM
    django-response对象
    spark集群进入 bin 下面目录./spark-shell 出现Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
    Hbase启动的时候出现:[RpcServer.handler=28,port=60000] ipc.RpcServer: RpcServer.handler=28,port=60000: exiting,master.HMasterCommandLine: Master exiting
    c++实现写一个函数,求2个整数的和,要求在函数体内不得使用+,-* /
    nutch二次开发环境搭建
  • 原文地址:https://www.cnblogs.com/huzhiwei/p/2369789.html
Copyright © 2020-2023  润新知