Database Reference
In-Depth Information
$sth
=
$dbh
->
prepare
(
$stmt
);
$sth
->
execute
();
printf
(
"Number of rows updated: %d
\n
"
,
$sth
->
rowCount
());
The PDO driver for MySQL returns rows-changed counts by default, but the driver
supports a
PDO::MYSQL_ATTR_FOUND_ROWS
attribute that you can specify at connect time
to control whether the server returns rows-changed or rows-matched counts. The
new
PDO
class constructor takes an optional key/value array following the password argu‐
ment. Pass
PDO::MYSQL_ATTR_FOUND_ROWS
=>
1
in this array to request rows-matched
counts:
$dsn
=
"mysql:host=localhost;dbname=cookbook"
;
$dbh
=
new
PDO
(
$dsn
,
"cbuser"
,
"cbpass"
,
array
(
PDO
::
MYSQL_ATTR_FOUND_ROWS
=>
1
));
Python
Python's DB API makes the rows-changed count available as the value of the statement
cursor's
rowcount
attribute:
cursor
=
conn
.
cursor
()
cursor
.
execute
(
stmt
)
print
(
"Number of rows affected:
%d
"
%
cursor
.
rowcount
)
cursor
.
close
()
To obtain rows-matched counts instead, import the Connector/Python client-flag con‐
stants and pass the
FOUND_ROWS
flag in the
client_flags
parameter of the
connect()
method:
from
mysql.connector.constants
import
ClientFlag
conn
=
mysql
.
connector
.
connect
(
database
=
"cookbook"
,
host
=
"localhost"
,
user
=
"cbuser"
,
password
=
"cbpass"
,
client_flags
=
[
ClientFlag
.
FOUND_ROWS
]
)
Java
For statements that modify rows, the Connector/J driver provides rows-matched counts
rather than rows-changed counts, for conformance with the Java JDBC specification.
The JDBC interface provides row counts two different ways, depending on the method
you invoke to execute the statement. If you use
executeUpdate()
, the row count is its
return value:
Statement
s
=
conn
.
createStatement
();
int
count
=
s
.
executeUpdate
(
stmt
);
s
.
close
();
System
.
out
.
println
(
"Number of rows affected: "
+
count
);