用于读取操作的 Ruby DBI 方法
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, ...] Returns all remaining rows of the result-set collected in an array. |
| 6 | sth.fetch_many( count ) => [aRow, ...] 返回在 [aRrow, ...] 数组中收集的下一个 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 释放prepared 语句持有的资源。 调用此方法后,无法对该对象调用其他方法。 |
方向参数
以下值可用于 fetch_scroll 方法的方向参数 −
| 序号 | Constant & 描述 |
|---|---|
| 1 | DBI::SQL_FETCH_FIRST 获取第一行。 |
| 2 | DBI::SQL_FETCH_LAST 获取最后一行。 |
| 3 | DBI::SQL_FETCH_NEXT 获取下一行。 |
| 4 | DBI::SQL_FETCH_PRIOR 获取上一行。 |
| 5 | DBI::SQL_FETCH_ABSOLUTE 在位置偏移处获取行。 |
| 6 | DBI::SQL_FETCH_RELATIVE 获取与当前偏移的行。 |
示例
以下示例显示如何获取语句的元数据。 考虑我们在上一章中创建的 EMPLOYEE 表。
#!/usr/bin/ruby -w
require "dbi"
begin
# connect to the MySQL server
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
# disconnect from server
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

