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: %dn", sth.rows
     else
        puts "Statement has a result set"
        rows = sth.fetch_all
        printf "Number of rows: %dn", rows.size
        printf "Number of columns: %dn", sth.column_names.size
        sth.column_info.each_with_index do |info, i|
          printf "--- Column %d (%s) ---n", i, info["name"]
          printf "sql_type:         %sn", info["sql_type"]
          printf "type_name:        %sn", info["type_name"]
          printf "precision:        %sn", info["precision"]
          printf "scale:            %sn", info["scale"]
          printf "nullable:         %sn", info["nullable"]
          printf "indexed:          %sn", info["indexed"]
          printf "primary:          %sn", info["primary"]
          printf "unique:           %sn", info["unique"]
          printf "mysql_type:       %sn", info["mysql_type"]
          printf "mysql_type_name:  %sn", info["mysql_type_name"]
          printf "mysql_length:     %sn", info["mysql_length"]
          printf "mysql_max_length: %sn", info["mysql_max_length"]
          printf "mysql_flags:      %sn", 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其他扩展