莊周夢蝶

          生活、程序、未來
             :: 首頁 ::  ::  :: 聚合  :: 管理

          ruby DBI安裝使用指南

          Posted on 2007-04-10 20:21 dennis 閱讀(4658) 評論(1)  編輯  收藏 所屬分類: 動態語言
          1.安裝
          首先,我是使用mysql進行測試的,你的機器上需要安裝mysql數據庫。
          然后執行:
          gem install mysql

          到rubyforge下載ruby-DBI,解壓后cd到目錄運行如下命令:
          ruby setup.rb config --with=dbi,dbd_mysql
          ruby setup.rb setup
          ruby setup.rb install

          完整的setup命令參數參考DBI的doc

          2.完整例子
          DBI是一類似于ODBC的開發式的統一的數據庫編程接口,結構層次上可以分為兩層:
          1.Database Interface——數據庫接口層,與數據庫無關,提供與數據庫無關的標準接口
          2.Database Driver——數據庫驅動,與數據庫相關

          DBI也是很簡單易用的,一個完整的使用例子,對于初學者可能有點幫助:
          require 'dbi'
          begin
            
          #連接數據庫
            dbh=DBI.connect("DBI:Mysql:dbi_test:localhost","root","")
            
            dbh.columns(
          "simple").each do |h|
              p h
            end
            
          #示范3種事務處理方式
            #手動commit
            dbh["AutoCommit"]=false
            
          1.upto(10) do |i|
              sql 
          = "insert into simple (name, author) VALUES (?, ?)"
              dbh.do(sql, 
          "Song #{i}""#{i}")
            end
            dbh.commit
            
            
          #使用transaction方法
            dbh.transaction do |dbh|
              
          1.upto(10) do |i|
                sql 
          = "insert into simple (name, author) VALUES (?, ?)"
                dbh.do(sql, 
          "Song #{i}""#{i}")
              end
            end
            
            
          #使用SQL語句
            dbh.do("SET AUTOCOMMIT=0")
            dbh.do(
          "BEGIN")
            dbh[
          "AutoCommit"]=false
            dbh.do(
          "UPDATE simple set name='test' where id='1'")
            dbh.do(
          "COMMIT")
            
            
          #查詢
            sth=dbh.execute("select count(id) from simple")
            puts 
          "bookCount:#{sth.fetch[0]}"
            sth.finish
            begin
              sth
          =dbh.prepare("select * from simple")
              sth.execute
              
          while row=sth.fetch do
                p row
              end
              sth.finish
            rescue
            end
            
            
          #上面這段查詢可以改寫為:
            #dbh.select_all("select * from simple") do |row|
            #   p row
            #end   
            
            
            
          #使用工具類輸出xml格式結果集以及測量查詢時間
            sql="select * from simple"
            mesuretime
          =DBI::Utils::measure do
              sth
          =dbh.execute(sql)
            end 
            puts 
          "SQL:#{sql}"
            puts 
          "Time:#{mesuretime}"
            rows
          =sth.fetch_all
            col_names
          =sth.column_names
            sth.finish
            puts DBI::Utils::XMLFormatter.table(rows)
            
            dbh.do(
          "delete from simple")
          rescue  DBI::DatabaseError
          =>e
            puts 
          "error code:#{e.err}"
            puts 
          "Error message:#{e.errstr}"
          ensure
            dbh.disconnect 
          if dbh
          end   



          評論

          # re: ruby DBI安裝使用指南  回復  更多評論   

          2008-06-26 22:58 by 唐遠見
          沒有出現中文亂碼的問題嘛?
          主站蜘蛛池模板: 灌阳县| 青岛市| 广宁县| 什邡市| 临颍县| 文成县| 九龙县| 温宿县| 新昌县| 喀什市| 清流县| 麻江县| 莎车县| 涞水县| 乐陵市| 长垣县| 泌阳县| 株洲市| 东阳市| 昌吉市| 错那县| 遂平县| 清流县| 乌拉特中旗| 北碚区| 横峰县| 昭苏县| 潢川县| 桦南县| 乌鲁木齐市| 永川市| 武穴市| 关岭| 瑞昌市| 盐源县| 报价| 鄂伦春自治旗| 阜平县| 横峰县| 修文县| 中宁县|