Database Reference
In-Depth Information
The
column_names
method returns an array of column names (which is empty if there
is no result set). If there is a result set, the
column_info
method returns an array of
ColumnInfo
objects, one for each column. A
ColumnInfo
object is similar to a hash and
has the elements shown in the following table. Element names that begin with
mysql_
are MySQL-specific and nonportable:
Element name Element meaning
name
Column name
sql_type
XOPEN type number
type_name
XOPEN type name
precision
Column width
scale
Number of decimal places (for numeric columns)
nullable
True if column permits
NULL
values
indexed
True if column is indexed
primary
True if column is part of a primary key
unique
True if column is part of a unique index
mysql_type
Data type (numeric internal MySQL code)
mysql_type_name
Data type name
mysql_length
Column width
mysql_max_length
Actual maximum length of column values in result set
mysql_flags
Data type flags
This example code shows how to execute a statement and display result set metadata:
stmt
=
"SELECT name, birth FROM profile"
puts
"Statement:
#{
stmt
}
"
sth
=
dbh
.
execute
(
stmt
)
# metadata information becomes available at this point ...
puts
"Number of columns:
#{
sth
.
column_names
.
size
}
"
puts
"Note: statement has no result set"
if
sth
.
column_names
.
size
==
0
sth
.
column_info
.
each_with_index
do
|
info
,
i
|
puts
"--- Column
#{
i
}
(
#{
info
[
'name'
]
}
) ---"
puts
"sql_type:
#{
info
[
'sql_type'
]
}
"
puts
"type_name:
#{
info
[
'type_name'
]
}
"
puts
"precision:
#{
info
[
'precision'
]
}
"
puts
"scale:
#{
info
[
'scale'
]
}
"
puts
"nullable:
#{
info
[
'nullable'
]
}
"
puts
"indexed:
#{
info
[
'indexed'
]
}
"
puts
"primary:
#{
info
[
'primary'
]
}
"
puts
"unique:
#{
info
[
'unique'
]
}
"
puts
"mysql_type:
#{
info
[
'mysql_type'
]
}
"
puts
"mysql_type_name:
#{
info
[
'mysql_type_name'
]
}
"
puts
"mysql_length:
#{
info
[
'mysql_length'
]
}
"
puts
"mysql_max_length:
#{
info
[
'mysql_max_length'
]
}
"
puts
"mysql_flags:
#{
info
[
'mysql_flags'
]
}
"