Database Handles and Statement Handles
DBI methods work on two different types of handles:
database handles and statement handles. A database handle is like a
filehandle; connect
is a DBI
class method that opens a connection to a database and returns a
database handle object:
$db_handle = DBI->connect(dbi:mSQL:bookdb, undef, undef) || die("Connect error: $DBI::errstr");
Statement handles are another thing entirely. DBI makes a
distinction between the preparation of SQL statements and their
execution by allowing you to preformat a statement into a statement
handle. You can prepare a statement with the prepare
method, which returns a statement
handle. You can then assign a SQL statement to the statement handle
via various statement handle methods and execute it with the
execute
method when you’re done.
(You can also prepare and execute in the same command with the
do
method.)
Changes to the database are written to the database automatically if the AutoCommit attribute is turned
on. If AutoCommit is off, use the commit
method when you’re ready to write
the changes to the database.
AutoCommit is only one of many attributes that can be set for
both database and statement handles. For example, if $st_handle
is a statement handle, you can
set $st_handle->{NULLABLE}
to
determine if the fields can contain null characters. Table 12-2 is a listing of
all the attributes supported by database handles, statement handles,
or both.
Table 12-2. Attributes for database and statement handles
Attribute ... |
---|
Get Perl in a Nutshell, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.