NAME
DBI - Database independent interface for Perl (DRAFT ONLY)
SYNOPSIS
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;
NOTE
This documentation is a new draft $Revision: 1.65 $ dated 3rd March 1997.
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.
DESCRIPTION
The Perl DBI is a database access Application Programming Interface (API) for the Perl Language. The DBI defines a set of functions, variables and conventions that provide a consistent database interface independant of the actual database being used.
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.
Architecture of a DBI Application
|<- 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.
Notation and Conventions
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}}
Data Query Methods
DBI allows the application to `prepare' a statement for later execution. A prepared statement is identified by a statement handle object, e.g., $sth.
Typical method call sequence (for a select statement):
prepare,
execute, fetch, fetch, ... finish,
execute, fetch, fetch, ... finish,
execute, fetch, fetch, ... finish.
Typical method call sequence (for a non-select statement):
prepare,
execute,
execute,
execute.
THE DBI CLASS
DBI Class Methods
- connect
-
$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 should check the value of
$dbh-
{AutoCommit}>.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.
- available_drivers
-
@ary = DBI->available_drivers;
Return a list of all available drivers
DBI Utility Functions
- neat
-
$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 wil...
if longer than $maxlen (0 or undef defaults to 400 characters). - neat_list
-
$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
", "
. - dump_results
-
$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.
DBI Dynamic Attributes
These attributes are always associated with the last handle used.
Where an attribute is Equivalent to a method call, then refer to the method call for all related documentation.
- $DBI::err
-
Equivalent to $h->err.
- $DBI::errstr
-
Equivalent to $h->errstr.
- $DBI::state
-
Equivalent to $h->state.
- $DBI::rows
-
Equivalent to $h->rows.
METHODS COMMON TO ALL HANDLES
- err
-
$rv = $dbh->err;
Returns the native database engine error code from the last driver function called.
- errstr
-
$str = $dbh->errstr;
Returns the native database engine error message from the last driver function called.
- state
-
$rv = $dbh->state;
Returns an error code in the standard SQLSTATE five character format. Note that the specific success code
00000
is translated to0
(false). If the driver does not support SQLSTATE then state will returnS1000
(General Error) for all errors.
ATTRIBUTES COMMON TO ALL HANDLES
- Warn
-
$h->{Warn}
Enables useful warnings for certain bad practices. Enabled by default. Some emulation layers, especially those for perl4 interfaces, disable warnings.
- CompatMode
-
$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.
- InactiveDestroy
-
$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.
DBI DATABASE HANDLE OBJECTS
Database Handle Methods
- prepare
-
Prepare a single statement for execution by the database engine and return a reference to a statement handle object which can be used to get attributes of the statement and invoke the $sth->execute method.
$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.
- do
-
$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.
- commit
-
$rc = $dbh->commit;
Commit (make permanent) the most recent series of database changes.
- rollback
-
$rc = $dbh->rollback;
Roll-back (undo) the most recent series of uncommited database changes.
- disconnect
-
$rc = $dbh->disconnect;
- quote
-
$sql = $dbh->quote($string);
Quote a string literal for use in an SQL statement by adding the required type of outer quotation marks and escaping any special characters (such as quotation marks) contained within the string.
$sql = sprintf "select foo from bar where baz = %s", $dbh->quote("Don't\n");
Database Handle Attributes
- AutoCommit
-
$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.
DBI STATEMENT HANDLE OBJECTS
Statement Handle Methods
- execute
-
$rc = $sth->execute;
Executes the prepared statement. returns undef, 0E0, 1, 2, ...
- fetch
-
$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.
- fetchrow
-
@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. - finish
-
$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 currently being held. It does not affect the transaction status of the session in any way. - rows
-
$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.
- bind_col
-
$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.
- bind_columns
-
$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.
Statement Handle Attributes
- NUM_OF_FIELDS
-
$sth->{NUM_OF_FIELDS} ($)
Number of fields (columns) the prepared statement will return.
- NUM_OF_PARAMS
-
$sth->{NUM_OF_PARAMS} ($)
The number of parameters (placeholders) in the prepared statement. See SUBSTITUTION VARIABLES below for more details.
- NAME
-
$sth->{NAME} (\@)
Array of field names for each column.
print "First column name: $sth->{NAME}->[0]\n";
- NULLABLE
-
$sth->{NULLABLE} (\@)
Array indicating the possibility of each column returning a null.
print "First column may return NULL\n" if $sth->{NULLABLE}->[0];
- CursorName
-
$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.
Bind Variables
Also known as place holders and substitution variables.
This section has not yet been formalised.
SIMPLE EXAMPLE
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";
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;
DEBUGGING
Detailed debugging can be enabled for a specific handle (and any future children of that handle) by executing
$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.
WARNINGS
The DBI is alpha software. It is only 'alpha' because the interface (api) is not finalised. The alpha status does not reflect code quality or stability.
SEE ALSO
Database Documentation
SQL Language Reference Manual.
Books and Journals
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.
Manual Pages
perl(1), perlmod(1), perlbook(1)
Mailing List
The dbi-users mailing list is the primary means of communication among uses of the DBI and its related modules. Subscribe and unsubscribe via:
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
Assorted Related WWW Links
The DBI 'Home Page' (not maintained by me):
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
AUTHORS
DBI by Tim Bunce. This pod text by Tim Bunce, J. Douglas Dunlop and others. Perl by Larry Wall and the <perl5-porters@perl.org>.
COPYRIGHT
The DBI module is Copyright (c) 1995,1996,1997 Tim Bunce. England. The DBI module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
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.
SUPPORT / WARRANTY
The DBI is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
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.
OUTSTANDING ISSUES
bind variables
blob_read
error handling
portability
etc
FREQUENTLY ASKED QUESTIONS
Why doesn't my CGI script work right?
Read http://www.perl.com/perl/faq/idiots-guide.html and other perl/CGI information at http://www.perl.com. Please do not post CGI related questions to the dbi-users mailing list (or to me).
How can I maintain a WWW connection to a database?
For information on the Apache httpd server and the mod_perl module see http://www.osf.org/~dougm/apache
A driver build fails because it can't find DBIXS.h
The installed location of the DBIXS.h file changed with 0.77 (it was being installed into the 'wrong' directory but that's where driver developers came to expect it to be). The first thing to do is check to see if you have the latest version of your driver. Driver authors will be releasing new versions which use the new location. If you have the latest then ask for a new release. You can edit the Makefile.PL file yourself. Change the part which reads "-I.../DBI" so it reads "-I.../auto/DBI" (where ... is a string of non-space characters).
What about ODBC?
See the statement and following notes in the DBI README file.
KNOWN DRIVER MODULES
- Oracle - DBD::Oracle
-
Author: Tim Bunce Email: dbi-users@fugue.com
- Ingres - DBD::Ingres
- mSQL - DBD::mSQL
- DB2 - DBD::DB2
- Empress - DBD::Empress
- Informix - DBD::Informix
-
Author: Jonathan Leffler Email: dbi-users@fugue.com
- Solid - DBD::Solid
-
Author: Thomas Wenrich Email: wenrich@site58.ping.at, dbi-users@fugue.com
- Postgres - DBD::Pg
-
Author: Edmund Mergl Email: mergl@nadia.s.bawue.de, dbi-users@fugue.com
OTHER RELATED MODULES
- Apache::DBI by E.Mergl@bawue.de
-
To be used with the Apache daemon together with an embedded perl interpreter like mod_perl. Establishes a database connection which remains open for the lifetime of the http daemon. This way the CGI connect and disconnect for every database access becomes superfluous.