Ruby DBI Read 操作
DBI 提供了一些從資料庫獲取記錄的不同方法。假設 dbh 是一個資料庫句柄,sth 是一個語句句柄:
序號 | 方法 & 描述 |
---|---|
1 | db.select_one( stmt, *bindvars ) => aRow | nil 執行帶有 bindvars 綁定在參數標記前的 stmt 語句。返回第一行,如果結果集為空則返回 nil。 |
2 | db.select_all( stmt, *bindvars ) => [aRow, ...] | nil db.select_all( stmt, *bindvars ){ |aRow| aBlock } 執行帶有 bindvars 綁定在參數標記前的 stmt 語句。調用不帶有塊的該方法,返回一個包含所有行的數組。如果給出了一個塊,則會為每行調用該方法。 |
3 | sth.fetch => aRow | nil 返回下一行。如果在結果中沒有下一行,則返回 nil。 |
4 | sth.fetch { |aRow| aBlock } 為結果集中剩餘的行調用給定的塊。 |
5 | sth.fetch_all => [aRow, ...] 返回保存在數組中的結果集的所有剩餘的行。 |
6 | sth.fetch_many( count ) => [aRow, ...] 返回保存在 [aRow, ...] 數組中的往下第 count 行。 |
7 | sth.fetch_scroll( direction, offset=1 ) => aRow | nil 返回 direction 參數和 offset 指定的行。除了 SQL_FETCH_ABSOLUTE 和 SQL_FETCH_RELATIVE,其他方法都會丟棄參數 offset。direction 參數可能的值,請查看下麵的表格。 |
8 | sth.column_names => anArray 返回列的名稱。 |
9 | column_info => [ aColumnInfo, ... ] 返回 DBI::ColumnInfo 對象的數組。每個對象存儲有關某個列的資訊,並包含該列的名稱、類型、精度等其他更多的資訊。 |
10 | sth.rows => rpc 返回執行語句處理的行數 Count,如果不存在則返回 nil。 |
11 | sth.fetchable? => true | false 如果可能獲取行,則返回 true,否則返回 false。 |
12 | sth.cancel 釋放結果集所佔有的資源。在調用該方法後,您就不能在獲取行了,除非再次調用 execute。 |
13 | sth.finish 釋放準備語句所佔有的資源。在調用該方法後,您就不能在該對象上調用其他進一步操作的方法了。 |
direction 參數
下麵的值可用於 fetch_scroll 方法的 direction 參數:
常量 | 描述 |
---|---|
DBI::SQL_FETCH_FIRST | 獲取第一行。 |
DBI::SQL_FETCH_LAST | 獲取最後一行。 |
DBI::SQL_FETCH_NEXT | 獲取下一行。 |
DBI::SQL_FETCH_PRIOR | 獲取上一行。 |
DBI::SQL_FETCH_ABSOLUTE | 獲取在該位置偏移處的行。 |
DBI::SQL_FETCH_RELATIVE | 獲取距離當前行該偏移量的行。 |
實例
下麵的實例演示了如何獲取一個語句的元數據。假設我們有 EMPLOYEE 表。
#!/usr/bin/ruby -w require "dbi" begin # 連接到 MySQL 伺服器 dbh = DBI.connect("DBI:Mysql:TESTDB:localhost", "testuser", "test123") sth = dbh.prepare("SELECT * FROM EMPLOYEE WHERE INCOME > ?") sth.execute(1000) if sth.column_names.size == 0 then puts "Statement has no result set" printf "Number of rows affected: %d\n", sth.rows else puts "Statement has a result set" rows = sth.fetch_all printf "Number of rows: %d\n", rows.size printf "Number of columns: %d\n", sth.column_names.size sth.column_info.each_with_index do |info, i| printf "--- Column %d (%s) ---\n", i, info["name"] printf "sql_type: %s\n", info["sql_type"] printf "type_name: %s\n", info["type_name"] printf "precision: %s\n", info["precision"] printf "scale: %s\n", info["scale"] printf "nullable: %s\n", info["nullable"] printf "indexed: %s\n", info["indexed"] printf "primary: %s\n", info["primary"] printf "unique: %s\n", info["unique"] printf "mysql_type: %s\n", info["mysql_type"] printf "mysql_type_name: %s\n", info["mysql_type_name"] printf "mysql_length: %s\n", info["mysql_length"] printf "mysql_max_length: %s\n", info["mysql_max_length"] printf "mysql_flags: %s\n", info["mysql_flags"] end end sth.finish rescue DBI::DatabaseError => e puts "An error occurred" puts "Error code: #{e.err}" puts "Error message: #{e.errstr}" ensure # 斷開與伺服器的連接 dbh.disconnect if dbh end
這將產生以下結果:
Statement has a result set Number of rows: 5 Number of columns: 5 --- Column 0 (FIRST_NAME) --- sql_type: 12 type_name: VARCHAR precision: 20 scale: 0 nullable: true indexed: false primary: false unique: false mysql_type: 254 mysql_type_name: VARCHAR mysql_length: 20 mysql_max_length: 4 mysql_flags: 0 --- Column 1 (LAST_NAME) --- sql_type: 12 type_name: VARCHAR precision: 20 scale: 0 nullable: true indexed: false primary: false unique: false mysql_type: 254 mysql_type_name: VARCHAR mysql_length: 20 mysql_max_length: 5 mysql_flags: 0 --- Column 2 (AGE) --- sql_type: 4 type_name: INTEGER precision: 11 scale: 0 nullable: true indexed: false primary: false unique: false mysql_type: 3 mysql_type_name: INT mysql_length: 11 mysql_max_length: 2 mysql_flags: 32768 --- Column 3 (SEX) --- sql_type: 12 type_name: VARCHAR precision: 1 scale: 0 nullable: true indexed: false primary: false unique: false mysql_type: 254 mysql_type_name: VARCHAR mysql_length: 1 mysql_max_length: 1 mysql_flags: 0 --- Column 4 (INCOME) --- sql_type: 6 type_name: FLOAT precision: 12 scale: 31 nullable: true indexed: false primary: false unique: false mysql_type: 4 mysql_type_name: FLOAT mysql_length: 12 mysql_max_length: 4 mysql_flags: 32768