Rails 連接 Oracle

阅读更多

地址:http://www.cine.idv.tw/Learning/ComputerScience/Networking/Servers/Rails/rails-902363a5-oracle?set_language=zh&cl=zh

 

說明 Rails 連接 Oracle 之方法。

Windows 平台

  1. 確認 Oracle 用戶端已經安裝,且可以正常的連線到 Rails 使用之資料庫

    這裡我們將使用 TNS 名稱為 oratest 的 Oracle 資料庫

  2. 安裝 Ruby/OCI8

    1. 下載

      http://rubyforge.org/projects/ruby-oci8/

    2. 安裝

      C:\>ruby ruby-oci8-0.1.14-mswin32.rb
      Copy OCI8.rb to d:/working/ruby/lib/ruby/site_ruby/1.8/DBD/OCI8
      Copy oci8.rb to d:/working/ruby/lib/ruby/site_ruby/1.8
      Copy oci8lib.so to d:/working/ruby/lib/ruby/site_ruby/1.8/i386-msvcrt
      OK?
      Enter Yes/No: Yes
      Copying OCI8.rb to d:/working/ruby/lib/ruby/site_ruby/1.8/DBD/OCI8 ... done
      Copying oci8.rb to d:/working/ruby/lib/ruby/site_ruby/1.8 ... done
      Copying oci8lib.so to d:/working/ruby/lib/ruby/site_ruby/1.8/i386-msvcrt ... don
      e
      OK
    3. 測試

      C:\>ruby -r oci8 -e "OCI8.new('erd', 'erd', 'oratest').exec('select sysdate from
      dual') do |r| puts r.join(','); end"
      2006/04/19 17:49:50
  3. 安裝 Ruby/DBI

    1. 下載

      http://rubyforge.org/projects/ruby-dbi/

    2. 安裝

      rem configure it (for this example only dbi itself is included): 
      c:\>ruby setup.rb config –with=dbi
      rem build:
      c:\> ruby setup.rb setup
      rem install:
      c:\> ruby setup.rb install
    3. 測試

      # 將以下的內容放到 ora_dbi.rb 檔案中
      require 'dbi'

      dbh = DBI.connect('DBI:OCI8:oratest', 'erd', 'erd')
      dbh.select_all('select sysdate from dual') do | row |
      p row
      end

      dbh.disconnect
      C:\>ora_dbi.rb
      [2006/04/19 17:57:22]
  4. 安裝 ActiveRecord
    1. 如果你安裝了 Rails , ActiveRecord 應該已經正確安裝。

    2. 測試

      # 將以下的內容存到 ora_activerecord.rb 檔案中
      require 'rubygems'
      require 'active_record'

      ActiveRecord::Base.establish_connection(
      :adapter => "oci",
      :username => "erd",
      :password => "erd",
      :host => "oratest")

      class TestTable < ActiveRecord::Base
      set_table_name "security_users"
      set_primary_key "name"
      end

      TestTable.find(:all, :conditions => ["name = ?", "Shortie"]).each do |tt|
      p tt
      end
      C:\>ora_activerecord.rb
      #nil, "name"=>"Shortie", "lan
      guage"=>nil, "password_status"=>"A", "priority"=>0, "description"=>"\254K\252\37
      0", "emp_id"=>nil, "user_type"=>0, "password_days"=>nil, "password"=>nil}>

註:在 database.yml 中的 adapter 使用 oci 。

你可能感兴趣的:(Rails,Oracle,Ruby,ActiveRecord,C#)