下面是一段ruby连接mysql数据库的典型例程:
# simple.rb - simple MySQL script using Ruby DBI module

   require "dbi"

   begin
       # connect to the MySQL server
       dbh = DBI.connect("dbi:Mysql:test:localhost", "root", "123")
       # get server version string and display it
       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

但是运行后会出现类似下面的错误:
... ...in `load_driver':  is not a cl
ass/module (TypeError)
        from c:/bin/ruby/lib/ruby/site_ruby/1.8/dbi/dbi.rb:401:in `_get_full_dri
ver'
        from c:/bin/ruby/lib/ruby/site_ruby/1.8/dbi/dbi.rb:381:in `connect'
        from test1.rb:5

解决的办法就是: gem install mysql
但是又出现了 mysqlclient.lib,客户端连接的问题,所以你还需要用以下方法来解决问题
1. Downloaded the mysql.so (as a zip file) from http://seagecko.org/thoughts/in-the-past/2004/09/09

2. Replaced the mysql.so file in the C:\ruby\lib\ruby\site_ruby\1.8\i386-msvcrt folder with the new file.

3. This mysql.so was compiled with mysql 4.0 libraries, so to satisfy the dependency, I went to http://www.mysql.com and downloaded the Windows version of mysql 4.0.25 (without installer) and unzipped it in a temporary folder. In the mysql/bin directory, I found the library file libmySQL.dll. I dropped the libmySQL.dll in my Windows/System32 directory and that was it.

当然你还需要msvcr70.dll文件。


这样上面的ruby程序就可以运行了。