论坛首页 编程语言技术论坛

(原创) Ruby 安装以及连接数据库测试

浏览 4676 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-04-19  
1.解压 InstantRails-1.6-win.zip 到 d:\rails\目录下
 删除 d:\rails\ruby 目录
2. 重新安装ruby:ruby185-21.exe
   安装到目录 d:\rails\ruby 目录下。
3. 安装rails 
 D:\rails\ruby>gem install rails --include-dependencies
4. 安装mysql
  D:\rails\ruby>gem install mysql
5. 测试连接 mysql
  D:\ruby>ruby connect_mysql.rb
 
6. 版本
(1)gem 版本:gem -v  0.9.0
(2)rail 版本:rails -v  Rails 1.2.3
(3)ruby 版本:ruby -v   ruby 1.8.5 (2006-08-25) [i386-mswin32]
7. 连接db2
D:\rails\ruby>gem install ibm_db
 
TEST
Note: IBM_DB Gem requires a manual step after install:
add ibm_db into gems\1.8\gems\activerecord-1.15.3\lib\active_record.rb (Line 77)
77 行:
改前:LS_CONNECTION_ADAPTERS = %w( mysql postgresql sqlite firebird sqlserver db2 oracle sybase openbase frontbase )
改后:LS_CONNECTION_ADAPTERS = %w( mysql postgresql sqlite firebird sqlserver db2 oracle sybase openbase frontbase ibm_db )
8. db2 连接测试
D:\rails>irb
irb(main):001:0> gem 'ibm_db'
=> true
irb(main):002:0> require 'mswin32/ibm_db'
=> true
irb(main):003:0> IBM_DB::connect 'mydb2', 'db2admin', 'db2admin'
=> #<IBM_DB::Connection:0x2c84900>
 
测试连接成功!!
 
 
 
附录:
# file name : connect_db2.rb
require "mswin32/ibm_db"
begin
conn = IBM_DB::connect 'mydb2','db2admin','db2admin'
 if conn
  printf("connected\n")
     result = IBM_DB::exec conn, "select id,name from wangyl.t_user"
     #print result
     while (row = IBM_DB::fetch_assoc(result))
   printf("%-10s ",row["ID"])
      printf("%-15s ",row["NAME"])
      puts ""
    end       
     else
        print "Connection failed."
     end
IBM_DB::close conn
end
 
connect_mysql.rb测试文件内容:
 
# file name : connect_mysql.rb
require "mysql"
begin
# connect to the   server
dbh = Mysql.real_connect("localhost", "root", "", "blog_development")
# get server version string and display it
puts "Your Server version: " + dbh.get_server_info
rescue MysqlError => e
print "Error code: ", e.errno, "\n"
print "Error message: ", e.error, "\n"
ensure
 
## 处理CRUD
dbh.query("drop table if exists test_rb")
dbh.query("create table test_rb(id int,name char(20))")
dbh.query("insert into test_rb values(1,'name1'),(2,'name2')")
printf "%d rows were inserted\n",dbh.affected_rows
 
## 处理查询R  each do
res = dbh.query("SELECT id,title FROM posts")
res.each do |row|
printf "%s, %s\n", row[0], row[1]
end
printf "%d rows were returned\n", res.num_rows
res.free
 
##处理查询R  while  使用下标
res = dbh.query("SELECT id,name FROM test_rb")
while row = res.fetch_row do
printf "%s, %s\n", row[0], row[1]
end
printf "%d rows were returned\n", res.num_rows
res.free
 
##处理查询R  while  使用字段名字
res = dbh.query("SELECT id,name FROM test_rb")
while row = res.fetch_hash do
printf "%s, %s\n", row["id"], row["name"]
end
printf "%d rows were returned\n", res.num_rows
res.free
 
##处理查询R  while  使用字段名字   each_hash是each的哈希版本
res = dbh.query("SELECT id,name FROM test_rb")
res.each_hash do |row|
printf "%s, %s\n", row["id"], row["name"]
end
printf "%d rows were returned\n", res.num_rows
res.free
 
# disconnect from server
dbh.close
end
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics