|
Last updated: April 22nd, 1997 This document was taken from http://www.hermetica.com. (Unix only)
NAMEDBI - Database independent interface for Perl (DRAFT ONLY)
SYNOPSISuse 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;
NOTEThis documentation is a new draft $Revision: 1.66 $ dated $Date: 1997/04/07 20:24:40 $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.
DESCRIPTIONThe 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 independent 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 ConventionsDBI 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}}
General Interface Rules & CaveatsThe DBI does not have a concept of a `current session'. Every session has a handle object (e.g., a $dbh) returned from the connect method and that handle object is used to invoke database related methods.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
Naming ConventionsThe DBI package and all packages below it (DBI::*) are are reserved for use by the DBI. Package names beginning with DBD:: are reserved for use by DBI database drivers. All environment variables used by the DBperl Switch or Adaptors begin with 'DBI_'.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.
Data Query MethodsThe DBI allows an 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:
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.
THE DBI CLASS
DBI Class Methods
DBI Utility Functions
DBI Dynamic AttributesThese 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.
METHODS COMMON TO ALL HANDLES
ATTRIBUTES COMMON TO ALL HANDLES
DBI DATABASE HANDLE OBJECTS
Database Handle Methods
Database Handle Attributes
DBI STATEMENT HANDLE OBJECTS
Statement Handle Methods
Statement Handle Attributes
Bind VariablesAlso known as place holders and substitution variables.This section has not yet been formalized.
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";
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;
DEBUGGINGDetailed debugging can be enabled for a specific handle (and any future children of that handle) by executing
$h->debug($level);
Where 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.
WARNINGSThe DBI is alpha software. It is only 'alpha' because the interface (api) is not finalized. The alpha status does not reflect code quality or stability.
SEE ALSO
Database DocumentationSQL Language Reference Manual.
Books and JournalsProgramming 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 Pagesperl(1), perlmod(1), perlbook(1)
Mailing ListThe 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 LinksThe 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
AUTHORSDBI by Tim Bunce. This pod text by Tim Bunce, J. Douglas Dunlop and others. Perl by Larry Wall and the perl5-porters.
ACKNOWLEDGEMENTSI would like to acknowledge the valuable contributions of the many people I have worked with on the DBI project, especially in the early years (1992-1994): Kevin Stock, Buzz Moschetti, Kurt Andersen, Ted Lemon, William Hails, Garth Kennedy, Michael Peppler, Neil S. Briscoe, David J. Hughes, Jeff Stander, Forrest D Whitcher, Larry Wall, Jeff Fried, Roy Johnson, Paul Hudson, Georg Rehfeld, Steve Sizemore, Ron Pool, Jon Meek, Tom Christiansen, Steve Baumgarten, Randal Schwartz, and a whole lot more.
COPYRIGHTThe 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 (please don't use this mail address for other DBI related mail - use the dbi-users mailing list).
SUPPORT / WARRANTYThe 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 ISSUESbind variables blob_read error handling portability data dictionary methods test harness support methods etc
FREQUENTLY ASKED QUESTIONS
Why doesn't my CGI script work right?Read the information in the references below. Please do not post CGI related questions to the dbi-users mailing list (or to me).
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/
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.hThe 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
OTHER RELATED MODULES
|