詳細(xì)探討Ruby使用DBI包裝器使用技巧
Ruby語言的應(yīng)用范圍非常廣泛,在實(shí)際應(yīng)用中,可以幫助我們實(shí)現(xiàn)許多功能的需求。在這里我們將會(huì)為大家詳細(xì)講解有關(guān)Ruby使用DBI包裝器的一些應(yīng)用技巧。#t#
理論上,DBI允許你以數(shù)據(jù)庫無關(guān)的方式存取你的數(shù)據(jù)庫.不管你的數(shù)據(jù)庫是Oracle, MySQL, PostgreSQL 還是其他,訪問的代碼都是一樣的。他有時(shí)是不能處理比較復(fù)雜的或則某個(gè)數(shù)據(jù)庫獨(dú)有的特性。
這里假設(shè)我們實(shí)現(xiàn)Ruby使用DBI包裝器時(shí)使用的是Oracle數(shù)據(jù)庫:
- require "dbi"
- db = DBI.connect("dbi:OCI8:
mydb", "user", "password")
query = "select * from people" - stmt = db.prepare(query)
- stmt.execute
- while row = stmt.fetch do
- puts row.join(",")
- end
- stmt.finish
- db.disconnect
Ruby使用DBI包裝器的應(yīng)用技巧就介紹到這里,希望大家能夠掌握這些內(nèi)容。