Use the Ruby DBI Module

Source: Internet
Author: User

Original DBI module installation Tutorial:

1.Install
First, I use mysql for testing. You need to install the mysql database on your machine.
Then execute:
Gem install mysql

Download ruby-DBI from rubyforge, decompress it, and run the following command in the cd directory:
Ruby setup. rb config -- with = dbi, dbd_mysql
Ruby setup. rb setup
Ruby setup. rb install

For the complete setup command parameters, refer to the DBI doc

2. Complete example
DBI is a uniform database programming interface similar to ODBC. It can be divided into two layers at the structural level:
1. Database Interface-Database Interface layer, which is unrelated to the Database and provides standard interfaces unrelated to the Database
2. Database Driver-Database Driver, related to the Database

DBI is also very easy to use. A complete example may be helpful to beginners:

Require 'dbi' begin # connect to the database dbh = dbi. connect ("DBI: Mysql: dbi_test: localhost", "root", "") dbh. columns ("simple "). each do | h | p h end # demonstrate three Transaction Processing Methods # manual 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 # Use the transaction method dbh. transaction do | dbh | 1. upto (10) do | I | SQL = "insert into simple (name, author) VALUES (?, ?) "Dbh. do (SQL, "Song # {I}", "# {I}") end # Use the SQL statement dbh. do ("set autocommit = 0") dbh. do ("BEGIN") dbh ["AutoCommit"] = false dbh. do ("UPDATE simple set name = 'test' where id = '1'") dbh. do ("COMMIT") # query sthsf-dbh.exe cute ("select count (id) from simple") puts "bookCount: # {something. fetch [0]} "something. finish begin… = dbh. prepare ("select * from simple") sth.exe cute while row = something. fetch do p row end. finish rescue end # The preceding query can be rewritten to: # dbh. select_all ("select * from simple ") do | row | # p row # end # use the tool class to output the xml format result set and measurement query time SQL = "select * from simple" mesuretime = DBI: Utils :: measure do sth=dbh.exe cute (SQL) end puts "SQL: # {SQL}" puts "Time: # {mesuretime}" rows = something. fetch_all col_names = something. column_names. 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
Problem:
But every time I perform the first step, config: unknown option -- with = dbi, dbd_mysql Try 'Ruby setup. rb -- help' for detailed usage will appear.
Cause analysis:
This is caused by dbi version problems.
ThereforeInstall the new versionThe method is as follows:

The general method is to download dbi(tar.gz or zip), switch to the decompressed directory under CMD, and use the following command:

Ruby setup. rb config (or ruby setup. rb config -- with = dbi, dbd_mysql)

Ruby setup. rb setup

Ruby setup. rb install

Error:

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>

This problem is relatively simple, as long as you install a deprecated package, so I downloaded the deprecated-3.0.0.gem, after installation still reported error.

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>

Again Google, it is deprecated-3.0.0.gem too new! Change to deprecated-2.0.1.gem.

irb(main):001:0> require 'dbi'  => true  irb(main):002:0> 


This problem is easy to solve, but it is difficult to solve the following problems:

<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>'

After the query, we found that the original parsedate has been abolished in ruby1.9.2. Now we use date. Therefore, there is really no way to solve this version problem. I am considering whether to downgrade ruby to solve this problem. Trying. If it is fixed later, it will be updated.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.