Ruby調用orcale資料庫

來源:互聯網
上載者:User

在Ruby中使用內建的資料庫函數調用orcale資料庫比較麻煩,所幸開源的東東拓展性很好,我們可以使用ruby-oci8這個開源的組件來對orcale資料庫進行一系列的操作。

安裝方法就不詳述了,具體可以參考http://ruby-oci8.rubyforge.org/en/InstallBinaryPackage.html,下面來看看如何操縱資料庫:

Require ‘oci8’

conn=OCI8.new(user_name,user_pwd,db_name)

cursor=conn.exec(”select * from student where id>10″)

while r=cursor.fetch()

arr=r.join()

end

cursor.close

conn.logoff

以上代碼實現了從資料庫中查詢資料的一個操作,並且把查詢出的結果儲存在arr這個數組裡。

使用oci8訪問orcale資料庫變得非常方便,首先用OCI8.new()方法建立一個資料庫聯結,然後使用exec()方法執行sql語句,例如select、updae、delete等等,不過要注意的是在執行更新,刪除,插入等操作後緊跟著要執行commit()方法來提交更改,不然是不會生效的。所以總的流程就是:

conn=OCI8.new(使用者名稱,密碼,資料庫名)

conn.exec(sql語句)

conn.commit

conn.logoff

其他還有很多有用的方法,詳情請參考http://ruby-oci8.rubyforge.org/en/api_OCI8.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.