print d rows were affected cursor.rowcount
9.2.7 Java
The Java JDBC int erface provides row count s t wo different ways, depending on t he m et hod you invoke t o execut e t he query. I f you use
executeUpdate
, it ret urns t he row count direct ly:
Statement s = conn.createStatement ; int count = s.executeUpdate query;
s.close ; System.out.println count + rows were affected;
I f you use
execute
, t hat m et hod ret urns t rue or false t o indicat e whet her or not t he st at em ent produces a result set . For st at em ent s such as
UPDATE
or
DELETE
t hat ret urn no result set , t he row count is available by calling t he
getUpdateCount
m et hod: Statement s = conn.createStatement ;
if s.execute query {
there is no result set, print the row count System.out.println s.getUpdateCount + rows were affected;
} s.close ;
For st at em ent s t hat m odify rows, t he MySQL Connect or J JDBC driver provides a rows- m at ched value for t he row count , rat her t han a rows-affect ed value.
9.3 Obtaining Result Set Metadata
9.3.1 Problem
You know how t o ret rieve t he rows of a result set , but you want t o know t hings about t he result , such as t he colum n nam es and dat a t ypes, or t he num ber of rows and colum ns t here
are.
9.3.2 Solution
Use t he appropriat e capabilit ies provided by your API .
9.3.3 Discussion
For queries t hat generat e a result set , you can get a num ber of kinds of m et adat a. This sect ion discusses t he inform at ion provided by each API , using program s t hat show how t o
display t he result set m et adat a available aft er issuing a sam ple query
SELECT name,
foods FROM
profile
. The sect ion also discusses som e applicat ions for t his inform at ion. One of t he sim plest uses is illust rat ed by several of t he exam ple program s: When you ret rieve a row of
values from a result set and you want t o process t hem in a loop, t he colum n count st ored in t he m et adat a serves as t he upper bound on t he loop it erat or.
9.3.4 Perl
Using t he DBI int erface, you can obt ain result set s t wo ways. These differ in t he scope of result set m et adat a available t o your script s:
•
Pr oce ss t h e qu e r y u sin g a st a t e m e n t h a n dle .
I n t his case, you invoke
prepare
t o get t he st at em ent handle, t hen call it s
execute
m et hod t o generat e t he result set , t hen fet ch t he rows in a loop. Wit h t his approach, access t o t he m et adat a is available while t he result set is act ive—t hat
is, aft er t he call t o
execute
and unt il t he end of t he result set is reached. When t he row- fet ching m et hod finds t hat t here are no m ore rows, it invokes
finish
im plicit ly, which causes t he m et adat a t o becom e unavailable. That also happens if you explicit ly call
finish
yourself. Thus, norm ally it s best t o access t he m et adat a im m ediat ely aft er calling
execute
, m aking a copy of any values t hat youll need t o use beyond t he end of t he fet ch loop.
•
Pr oce ss t h e qu e r y u sin g a da t a ba se h a n dle m e t h od t h a t r e t u r n s t h e r e su lt se t in a sin gle ope r a t ion .
Wit h t his m et hod, any m et adat a generat ed while processing t he query will have been disposed of by t he t im e t he m et hod ret urns, alt hough you can st ill det erm ine t he
num ber of rows and colum ns from t he size of t he result set . When you use t he st at em ent handle approach t o process a query, DBI m akes result set
m et adat a available aft er you invoke t he handles
execute
m et hod. This inform at ion is available prim arily in t he form of references t o arrays. There is a separat e array for each t ype
of m et adat a, and each array has one elem ent per colum n in t he result set . Array references are accessed as at t ribut es of t he st at em ent handle. For exam ple,
sth-{NAME}
point s t o t he colum n nam e array. I ndividual colum n nam es are available as elem ent s of t his array:
name = sth-{NAMES}-[i]; Or you can access t he ent ire array like t his:
names = {sth-{NAMES}}; The following t able list s t he at t ribut e nam es t hrough which you access array- based m et adat a
and t he m eaning of values in each array. Nam es t hat begin wit h uppercase are st andard DBI at t ribut es and should be available for m ost dat abase engines. At t ribut e nam es t hat begin wit h
mysql_
are MySQL- specific and non- port able; t he kinds of inform at ion t hey provide m ay be available in ot her dat abases, but under different at t ribut e nam es.
At t r ibu t e n a m e Ar r a y e le m e n t m e a n in g
NAME
Colum n nam e
NAME_lc
Colum n nam e, lowercased
NAME_uc
Colum n nam e, uppercased
NULLABLE
1 if colum n values can be
NULL
, em pt y st ring if not
PRECISION
Colum n widt h
SCALE
Num ber of decim al places for num eric colum ns
TYPE
Num eric colum n t ype DBI value
mysql_is_blob
True if colum n has a
BLOB
or
TEXT
t ype
mysql_is_key
True if colum n is part of a non-unique key
mysql_is_num
True if colum n has a num eric t ype
mysql_is_pri_key
True if colum n is part of a prim ary key
mysql_max_length
Act ual m axim um lengt h of colum n values in result set
mysql_table
Nam e of t able t he colum n is part of
mysql_type
Num eric colum n t ype int ernal MySQL value
mysql_type_name
Colum n t ype nam e The except ion t o array-based m et adat a is t hat t he num ber of colum ns in a result set is
available as a scalar value: num_cols = sth-{NUM_OF_FIELDS};
Heres som e exam ple code t hat shows how t o execut e a query and display t he result set m et adat a:
my query = SELECT name, foods FROM profile; printf Query: s\n, query;
my sth = dbh-prepare query; sth-execute ;
metadata information becomes available at this point ... printf NUM_OF_FIELDS: d\n, sth-{NUM_OF_FIELDS};
print Note: query has no result set\n if sth-{NUM_OF_FIELDS} == 0; for my i 0 .. sth-{NUM_OF_FIELDS}-1
{ printf --- Column d s ---\n, i, sth-{NAME}-[i];
printf NAME_lc: s\n, sth-{NAME_lc}-[i]; printf NAME_uc: s\n, sth-{NAME_uc}-[i];
printf NULLABLE: s\n, sth-{NULLABLE}-[i]; printf PRECISION: s\n, sth-{PRECISION}-[i];
printf SCALE: s\n, sth-{SCALE}-[i]; printf TYPE: s\n, sth-{TYPE}-[i];
printf mysql_is_blob: s\n, sth-{mysql_is_blob}-[i]; printf mysql_is_key: s\n, sth-{mysql_is_key}-[i];
printf mysql_is_num: s\n, sth-{mysql_is_num}-[i]; printf mysql_is_pri_key: s\n, sth-{mysql_is_pri_key}-[i];
printf mysql_max_length: s\n, sth-{mysql_max_length}-[i]; printf mysql_table: s\n, sth-{mysql_table}-[i];
printf mysql_type: s\n, sth-{mysql_type}-[i];
printf mysql_type_name: s\n, sth-{mysql_type_name}-[i]; }
sth-finish ; release result set, since we didnt fetch its rows
I f you use t he preceding code t o execut e t he query
SELECT name,
foods FROM
profile
, t he out put looks like t his:
Query: SELECT name, foods FROM profile NUM_OF_FIELDS: 2
--- Column 0 name --- NAME_lc: name
NAME_uc: NAME NULLABLE:
PRECISION: 20 SCALE: 0
TYPE: 1 mysql_is_blob:
mysql_is_key: mysql_is_num: 0
mysql_is_pri_key: mysql_max_length: 7
mysql_table: profile mysql_type: 254
mysql_type_name: char --- Column 1 foods ---
NAME_lc: foods NAME_uc: FOODS
NULLABLE: 1 PRECISION: 42
SCALE: 0 TYPE: 1
mysql_is_blob: mysql_is_key:
mysql_is_num: 0 mysql_is_pri_key:
mysql_max_length: 21 mysql_table: profile
mysql_type: 254 mysql_type_name: char
To get a row count from a result set generat ed by calling
execute
, you m ust fet ch t he rows and count t hem yourself. The use of
sth-rows
t o get a count for
SELECT
st at em ent s is specifically deprecat ed in t he DBI docum ent at ion. You can also obt ain a result set by calling one of t he DBI m et hods t hat uses a dat abase handle
rat her t han a st at em ent handle, such as
selectall_arrayref
or
selectall_hashref
. For t hese m et hods, no access t o colum n m et adat a is provided. That inform at ion already will have been disposed of by t he t im e t he m et hod ret urns, and is
unavailable t o your script s. However, you can st ill derive colum n and row count s by exam ining t he result set it self. The way you do t his depends on t he kind of dat a st ruct ure a m et hod
produces. These st ruct ures and t he way you use t hem t o obt ain result set row and colum n count s are discussed in
Recipe 2.5 .
9.3.5 PHP