DBI Specification
Last updated: April 22nd, 1997
use DBI;
$dbh = DBI->connect($database, $username, $auth); $dbh = DBI->connect($database, $username, $auth, $driver); $dbh = DBI->connect($database, $username, $auth, $driver, \%attr);
$rc = $dbh->do($statement); $rc = $dbh->do($statement, \%attr);
$sth = $dbh->prepare($statement); $sth = $dbh->prepare($statement, \%attr);
$rc = $sth->execute;
@row_ary = $sth->fetchrow; $row_ref = $sth->fetch;
$rc = $sth->finish;
$rv = $sth->rows;
$rc = $dbh->disconnect;
$sql = $dbh->quote($string);
$rv = $h->err; $str = $h->errstr; $rv = $h->state;
It is expected to evolve and expand quite quickly (relative to previous drafts :-) so it is important to check that you have the latest copy.
It is important to remember that the DBI is just an interface. A thin layer of 'glue' between an application and one or more Database Drivers. It is the drivers which do the real work. The DBI provides a standard interface and framework for the drivers to operate within.
This document is a work-in-progress. Although it is incomplete it should be useful in getting started with the DBI.
|<- Scope of DBI ->|
.-. .--------------. .-------------.
.-------. | |---| XYZ Driver |---| XYZ Engine |
| Perl | |S| `--------------' `-------------'
| script| |A| |w| .--------------. .-------------.
| using |--|P|--|i|---|Oracle Driver |---|Oracle Engine|
| DBI | |I| |t| `--------------' `-------------'
| API | |c|...
|methods| |h|... Other drivers
`-------' | |...
`-'
The API is the Application Perl-script (or Programming) Interface. The call interface and variables provided by DBI to perl scripts. The API is implemented by the DBI Perl extension.
The Switch is the code that 'dispatches' the DBI method calls to the appropriate Driver for actual execution. The Switch is also responsible for the dynamic loading of Drivers, error checking/handling and other general duties.
The Drivers implement support for a given type of Engine (database). Drivers contain implementations of the DBI methods written using the private interface functions of the corresponding Engine. Only authors of sophisticated/multi-database applications or generic library functions need be concerned with Drivers.
DBI static 'top-level' class name $dbh Database handle object $sth Statement handle object $drh Driver handle object (rarely seen or used in applications) $h Any of the $??h handle types above $rc General function/method Return Code (typically boolean: true/false) $rv General function/method Return Value (typically an integer) @ary List of values returned from the database, typically a row of data $rows Number of rows processed by a function (if available, else -1) $fh A filehandle undef NULL values are represented by undefined values in perl
Note that Perl will automatically destroy database and statement objects if all references to them are deleted.
Handle object attributes are shown as:
$h->{attribute_name} (I<type>)
where type indicates the type of the value of the attribute (if it's not a simple scalar):
\$ reference to a scalar: $h->{attr} or $a = ${$h->{attr}}
\@ reference to a list: $h->{attr}->[0] or @a = @{$h->{attr}}
\% reference to a hash: $h->{attr}->{a} or %a = %{$h->{attr}}
Most data is returned to the perl script as perl strings (null values are returned as undef). This allows arbitrary precision numeric data to be handled without loss of accuracy. Be aware that perl may not preserve the same accuracy when the string is used as a number.
Dates and times are returned as character strings in the native format of the corresponding Engine. Time Zone effects are Engine/Driver dependent.
Perl supports binary data in perl strings and the DBI will pass binary data to and from the Driver without change. It is up to the Driver implementors to decide how they wish to handle such binary data.
Multiple SQL statements may not be combined in a single statement handle, e.g., a single $sth.
Non-sequential record reads are not supported in this version of the DBI. E.g., records can only be fetched in the order that the database returned them and once fetched they are forgotten.
Positioned updates and deletes are not directly supported by the DBI. See the description of the CursorName attribute for an alternative.
Individual Driver implementors are free to provide any private functions
and/or handle attributes that they feel are useful. Private functions can
be invoked using the DBI call method. Private attributes are accessed just like standard attributes.
The letter case used for attribute names is significant and plays an important part in the portability of DBI scripts. The case of the attribute name is used to signify who defined the meaning of that name and its values.
Case of name Has a meaning defined by ------------ ------------------------ UPPER_CASE Standards, e.g., X/Open, SQL92 etc (portable) MixedCase DBI API (portable), underscores are not used. lower_case Driver or Engine specific (non-portable)
It is of the utmost importance that Driver developers only use lowercase attribute names when defining private attributes.
Typical method call sequence for a select statement:
connect,
prepare,
execute, fetch, fetch, ... finish,
execute, fetch, fetch, ... finish,
execute, fetch, fetch, ... finish.
Typical method call sequence for a non-select statement:
connect,
prepare,
execute,
execute,
execute.
$dbh = DBI->connect($database, $username, $password, $driver); $dbh = DBI->connect($database, $username, $password, $driver, \%attr);
Establishes a database connection (session) to the requested database.
Returns a database handle object. DBI->connect installs the requested driver if it has not been installed yet. It then returns the result of $drh->connect. It is important to note that driver installation always returns a valid driver handle or it dies with an error message which includes the string 'install_driver' and the underlying problem. So, DBI->connect will die on a driver installation failure and will only return undef on a connect failure, for which $DBI::errstr will hold the error.
The $database, $username and $password arguments
are passed to the driver for processing. The DBI does not define ANY
interpretation for the contents of these fields. As a convenience, if the
$database field is undefined or empty the Switch will
substitute the value of the environment variable DBI_DBNAME if any.
If $driver is not specified, the environment variable
DBI_DRIVER is used. If that variable is not set and the Switch has more
than one driver loaded then the connect fails and undef is returned.
The driver is free to interpret the database, username and password fields in any way and supply whatever defaults are appropriate for the engine being accessed.
Portable applications should not assume that a single driver will be able
to support multiple simultaneous sessions and also should check the value
of $dbh-{AutoCommit}>.
Where possible each session ($dbh) is independent from the transactions in other sessions. This is useful where you need to hold cursors open across transactions, e.g., use one session for your long lifespan cursors (typically read-only) and another for your short update transactions.
@ary = DBI->available_drivers;
Return a list of all available drivers
$str = DBI::neat($value, $maxlen);
Return a string containing a neat (and tidy) representation of the supplied
value. Strings will be quoted and undefined (NULL) values will be shown as undef. Unprintable characters will be replaced by dot (.) and the string will be
truncated and terminated with '...' if longer than $maxlen (0
or undef defaults to 400 characters).
$str = DBI::neat_list(\@listref, $maxlen, $field_sep);
Calls DBI::neat on each element of the list and returns a string containing
the results joined with $field_sep. $field_sep defaults to ", ".
$rows = DBI::dump_results($sth, \@listref, $maxlen, $lsep, $fsep, $fh);
Fetches all the rows from $sth, calls DBI::neat_list for each row and
prints the results to $fh (defaults to STDOUT) separated by $lsep (default "\n"). $fsep defaults to ", " and $maxlen defaults to 35. This function is designed as a
handy utility for prototyping and testing queries.
Where an attribute is Equivalent to a method call, then refer to the method call for all related documentation.
$rv = $dbh->err;
Returns the native database engine error code from the last driver function called.
$str = $dbh->errstr;
Returns the native database engine error message from the last driver function called.
$rv = $dbh->state;
Returns an error code in the standard SQLSTATE five character format. Note
that the specific success code 00000 is translated to
(false). If the driver does not support SQLSTATE then state will return S1000 (General Error) for all errors.
$h->{Warn}
Enables useful warnings for certain bad practices. Enabled by default. Some emulation layers, especially those for perl4 interfaces, disable warnings.
$h->{CompatMode}
Used by emulation layers (such as Oraperl) to enable compatible behaviour in the underlying driver (e.g., DBD::Oracle) for this handle. Not normally set by application code.
$h->{InactiveDestroy}
This attribute can be used to disable the effect of destroying a handle (which would normally close a prepared statement or disconnect from the database etc). It is specifically designed for use in unix applications which 'fork' child processes. Either the parent or the child process, but not both, should set InactiveDestroy on all their handles.
$sth = $dbh->prepare($statement); $sth = $dbh->prepare($statement, \%attr);
Drivers for engines which don't have the concept of preparing a statement will typically just store the statement in the returned handle and process it when $sth->execute is called. Such drivers are likely to be unable to give much useful information about the statement, such as $sth->{NUM_OF_FIELDS}, until after $sth->execute has been called. Prepare never executes the statement, even if it is not a select statement.
$rc = $dbh->do($statement); $rc = $dbh->do($statement, \%attr); $rc = $dbh->do($statement, \%attr, @bind_params);
Prepare and execute a statement. This method is typically most useful for non-select statements which either cannot be prepared in advance (due to a limitation in the driver) or which do not need to be executed repeatedly.
$rc = $dbh->commit;
Commit (make permanent) the most recent series of database changes if the database supports transactions.
$rc = $dbh->rollback;
Roll-back (undo) the most recent series of uncommited database changes if the database supports transactions.
$rc = $dbh->disconnect;
Disconnects the database from the database handle. Typically only used before exiting the program. The handle is of little use after disconnecting.
The transaction behaviour is of disconnect is undefined. Applications should explicitly call commit or rollback before calling disconnect.
The database is automatically disconnected (by the DESTROY method) if still connected when there are no longer any references to the handle. The DESTROY method for each driver should explicitly call rollback to undo any uncommited changes. This is vital behaviour to ensure that incomplete transactions don't get commited simply because Perl calls DESTROY on every object before exiting.
$sql = $dbh->quote($string);
Quote a string literal for use in an SQL statement by escaping any special characters (such as quotation marks) contained within the string and adding the required type of outer quotation marks.
$sql = sprintf "select foo from bar where baz = %s",
$dbh->quote("Don't\n");
For Oracle quote would return 'Don''t' and for Ingres it would return
'Don'+X'27+'t' (including the outer quotation marks).
$sth->{AutoCommit} ($)
If true then database changes cannot be rolledback (undone). If false then database changes occur within a 'transaction' which must either be commited or rolledback using the commit or rollback methods.
Drivers for databases which support transactions should always default to AutoCommit mode.
Some drivers only support AutoCommit mode and thus after an application sets AutoCommit it should check that it now has the desired value. All portable applications must explicitly set and check for the desired AutoCommit mode.
$rc = $sth->execute;
Perform whatever processing is necessary to execute the prepared statement. An undef is returned if an error occurs.
For a non-select statement execute returns the number of rows affected (if available). Zero rows is returned as ``0E0'' which Perl will treat as 0 but will regard as true.
For select statements execute simply 'starts' the query within the Engine. Use one of the fetch methods to retreive the data. Note that the execute method does not return the number of rows that will be returned by the query (because for most Engines it can't tell in advance).
$ary_ref = $sth->fetch;
Fetches the next row of data and returns a reference to an array holding the field values. If there are no more rows fetch returns undef. Null values are returned as undef.
@ary = $sth->fetchrow;
An alternative to fetch. Fetches the next row of data and returns it as an array holding the field values. If there are no more rows fetchrow returns an empty list. Null values are returned as undef.
$rc = $sth->finish;
Indicates that no more data will be fetched from this statement before it is either prepared again via prepare or destroyed. It is helpful to call this method where appropriate in order to allow the server to free off any internal resources (such as read locks) currently being held. It does not affect the transaction status of the session in any way.
$rv = $sth->rows;
Returns the number of rows affected by the last database altering command, or -1 if not known or available.
Generally you can only rely on a row count after a do or
non-select execute. Some drivers only offer a row count after
executing some specific operations (e.g., update and delete).
It is generally not possible to know how many rows will be returned from an arbitrary select statement except by fetching and counting them. Also note that some drivers, such as DBD::Oracle, implement read-ahead row caches for select statements which means that the row count may be incorrect while there are still more records to fetch.
$rv = $sth->bind_col($column_number, \$var_to_bind); $rv = $sth->bind_col($column_number, \$var_to_bind, \%attr);
Binds a column (field) of a select statement to a perl variable. Whenever a row is fetched from the database the corresponding perl variable is automatically updated. There is no need to fetch and assign the values manually. See bind_columns below for an example. Note that column numbers count up from 1.
The binding is performed at a very low level using perl aliasing so there is no extra copying taking place. So long as the driver uses the correct internal DBI call to get the array the fetch function returns it will automatically support column binding.
$rv = $sth->bind_columns(\%attr, @refs_to_vars_to_bind);
e.g.
$sth->prepare(q{ select region, sales from sales_by_region }) or die ...;
my($region, $sales);
# Bind perl variables to columns. Note use of perl's handy \(...) syntax.
$rv = $sth->bind_columns(undef, \($region, $sales));
# Column binding is the most eficient way to fetch data
while($sth->fetch) {
print "$region: $sales\n";
}
Calls bind_col for each column of the select statement. bind_columns will croak if the number of references does not match the number of fields.
$sth->{NUM_OF_FIELDS} ($)
Number of fields (columns) the prepared statement will return. Non-select statements will have NUM_OF_FIELDS == 0.
$sth->{NUM_OF_PARAMS} ($)
The number of parameters (placeholders) in the prepared statement. See SUBSTITUTION VARIABLES below for more details.
$sth->{NAME} (\@)
Array of field names for each column.
print "First column name: $sth->{NAME}->[0]\n";
$sth->{NULLABLE} (\@)
Array indicating the possibility of each column returning a null.
print "First column may return NULL\n" if $sth->{NULLABLE}->[0];
$sth->{CursorName} ($)
Returns the name of the cursor associated with the statement handle if
available. If not available or the database driver does not support the
"where current of ..." SQL syntax then it returns undef.
This section has not yet been formalised.
my $dbh = DBI->connect($database, $user, $password, 'Oracle')
or die "Can't connect to $database: $DBI::errstr";
my $sth = $dbh->prepare( q{
SELECT name, phone
FROM mytelbook
}) or die "Can't prepare statement: $DBI::errstr";
my $rc = $sth->execute
or die "Can't execute statement: $DBI::errstr";
print "Query will return $sth->{NUM_FIELDS} fields.\n\n";
print "$sth->{NAME}->[0]: $sth->{NAME}->[1]\n";
while (($name, $phone) = $sth->fetchrow()) {
print "$name: $phone\n";
}
# check for problems which may have terminated the fetch early
warn $DBI::errstr if $DBI::err;
$sth->finish;
$h->debug($level);
Where $level is at least 2 (recommended). Disable with
$level==0;
You can also enable debugging by setting the PERL_DBI_DEBUG environment variable to the same values. On unix-like systems using a bourne-like shell you can do this easily for a single command:
PERL_DBI_DEBUG=2 perl your_test_script.pl
The debugging output is detailed and typically very useful.
Programming Perl 2nd Ed. by Larry Wall, Tom Christiansen & Randal Schwartz. Learning Perl by Randal Schwartz.
Dr Dobb's Journal, November 1996. The Perl Journal, April 1997.
http://www.fugue.com/dbi
Mailing list archives are held at:
http://www.rosat.mpe-garching.mpg.de/mailing-lists/PerlDB-Interest/ http://www.coe.missouri.edu/~faq/lists/dbi.html
http://www.hermetica.com/technologia/DBI
Other related links:
http://www-ccs.cs.umass.edu/db.html http://www.odmg.org/odmg93/updates_dbarry.html http://www.jcc.com/sql_stnd.html ftp://alpha.gnu.ai.mit.edu/gnu/gnusql-0.7b3.tar.gz
This document is Copyright (c) 1997 by Tim Bunce. All rights reserved. Permission to distribute this document, in full or part, via email, usenet or ftp/http archives or printed copy is granted providing that no charges are involved, reasonable attempt is made to use the most current version, and all credits and copyright notices are retained. Requests for other distribution rights, including incorporation in commercial products, such as books, magazine articles, or CD-ROMs should be made to Tim.Bunce@ig.co.uk (please don't use this mail address for other DBI related mail - use the dbi-users mailing list).
Commercial support agreements for Perl and the DBI, DBD::Oracle and Oraperl modules can be arranged via The Perl Clinic. See http://www.perl.co.uk/tpc for more details.
bind variables blob_read error handling portability data dictionary methods test harness support methods etc
http://www.perl.com/perl/faq/idiots-guide.html http://www3.pair.com/webthing/docs/cgi/faqs/cgifaq.shtml http://www.perl.com/perl/faq/perl-cgi-faq.html http://www-genome.wi.mit.edu/WWW/faqs/www-security-faq.html http://www.boutell.com/faq/ http://www.perl.com/perl/faq/
"-I.../DBI" so it reads
"-I.../auto/DBI" (where ... is a string of non-space characters).
Author: Tim Bunce Email: dbi-users@fugue.com
Author: Jonathan Leffler Email: dbi-users@fugue.com
Author: Thomas Wenrich Email: wenrich@site58.ping.at, dbi-users@fugue.com
Author: Edmund Mergl Email: mergl@nadia.s.bawue.de, dbi-users@fugue.com