菜鸟教程 -- 学的不仅是技术,更是梦想!

Ruby 教程
(追記) (追記ここまで)

Ruby DBI Read 操作

DBI 提供了一些从数据库获取记录的不同方法。假设 dbh 是一个数据库句柄,sth 是一个语句句柄:

序号方法 & 描述
1db.select_one( stmt, *bindvars ) => aRow | nil
执行带有 bindvars 绑定在参数标记前的 stmt 语句。返回第一行,如果结果集为空则返回 nil
2db.select_all( stmt, *bindvars ) => [aRow, ...] | nil

db.select_all( stmt, *bindvars ){ |aRow| aBlock }

执行带有 bindvars 绑定在参数标记前的 stmt 语句。调用不带有块的该方法,返回一个包含所有行的数组。如果给出了一个块,则会为每行调用该方法。
3sth.fetch => aRow | nil
返回下一行。如果在结果中没有下一行,则返回 nil
4sth.fetch { |aRow| aBlock }
为结果集中剩余的行调用给定的块。
5sth.fetch_all => [aRow, ...]
返回保存在数组中的结果集的所有剩余的行。
6sth.fetch_many( count ) => [aRow, ...]
返回保存在 [aRow, ...] 数组中的往下第 count 行。
7sth.fetch_scroll( direction, offset=1 ) => aRow | nil
返回 direction 参数和 offset 指定的行。除了 SQL_FETCH_ABSOLUTE 和 SQL_FETCH_RELATIVE,其他方法都会丢弃参数 offsetdirection 参数可能的值,请查看下面的表格。
8sth.column_names => anArray
返回列的名称。
9column_info => [ aColumnInfo, ... ]
返回 DBI::ColumnInfo 对象的数组。每个对象存储有关某个列的信息,并包含该列的名称、类型、精度等其他更多的信息。
10sth.rows => rpc
返回执行语句处理的行数 Count,如果不存在则返回 nil
11sth.fetchable? => true | false
如果可能获取行,则返回 true,否则返回 false
12sth.cancel
释放结果集所占有的资源。在调用该方法后,您就不能在获取行了,除非再次调用 execute
13sth.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
AI 思考中...

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /