prepare_cached
$sth = $dbh->prepare_cached($statement) $sth = $dbh->prepare_cached($statement, \%attr) $sth = $dbh->prepare_cached($statement, \%attr, $allow_active)
Like
prepare
except that the statement handle returned will be stored in a hash
associated with the $dbh
. If another call is made
to prepare_cached
with the same
$statement
and %attr
values,
then the corresponding cached $sth
will be
returned without contacting the database server.
This caching can be useful in some applications, but it can also
cause problems and should be used with care. A warning will be
generated if the cached $sth
being returned is
active (i.e., it is a SELECT
that may still have
data to be fetched). This warning can be suppressed by setting
$allow_active
to true. The cache can be accessed
(and cleared) via the CachedKids
attribute.
Here’s an example of one possible use of
prepare_cached
:
while ( ($field, $value) = each %search_fields ) { push @sql, "$field = ?"; push @values, $value; } $qualifier = ""; $qualifier = "where ".join(" and ", @sql) if @sql; $sth = $dbh->prepare_cached("SELECT * FROM table $qualifier"); $sth->execute(@values);