17th



NAME

DBI - Database independent interface for Perl


SYNOPSIS

  use DBI;
  @driver_names = DBI->available_drivers;
  @data_sources = DBI->data_sources($driver_name);
  $dbh = DBI->connect($data_source, $username, $auth, \%attr);
  $rv  = $dbh->do($statement);
  $rv  = $dbh->do($statement, \%attr);
  $rv  = $dbh->do($statement, \%attr, @bind_values);
  $ary_ref = $dbh->selectall_arrayref($statement);
  @row_ary = $dbh->selectrow_array($statement);
  $ary_ref = $dbh->selectcol_arrayref($statement);
  $sth = $dbh->prepare($statement);
  $sth = $dbh->prepare_cached($statement);
  $rv = $sth->bind_param($p_num, $bind_value);
  $rv = $sth->bind_param($p_num, $bind_value, $bind_type);
  $rv = $sth->bind_param($p_num, $bind_value, \%attr);
  $rv = $sth->execute;
  $rv = $sth->execute(@bind_values);
  $rc = $sth->bind_col($col_num, \$col_variable);
  $rc = $sth->bind_columns(@list_of_refs_to_vars_to_bind);
  @row_ary  = $sth->fetchrow_array;
  $ary_ref  = $sth->fetchrow_arrayref;
  $hash_ref = $sth->fetchrow_hashref;
  $ary_ref  = $sth->fetchall_arrayref;
  $rv  = $sth->rows;
  $rc  = $dbh->commit;
  $rc  = $dbh->rollback;
  $sql = $dbh->quote($string);
  $rc  = $h->err;
  $str = $h->errstr;
  $rv  = $h->state;
  $rc  = $dbh->disconnect;

The synopsis above only lists the major methods.

GETTING HELP

If you have questions about DBI, you can get help from the dbi-users@isc.org mailing list. You can subscribe to the list by visiting:

  http://www.isc.org/dbi-lists.html

Also worth a visit is the DBI home page at:

  http://www.symbolstone.org/technology/perl/DBI

Before asking any questions, reread this document, consult the archives and read the DBI FAQ. The archives are listed at the end of this document. The FAQ is installed as a DBI::FAQ module so you can read it by executing perldoc DBI::FAQ.

Please note that Tim Bunce does not maintain the mailing lists or the web page (generous volunteers do that). So please don't send mail directly to him; he just doesn't have the time to answer questions personally. The dbi-users mailing list has lots of experienced people who should be able to help you if you need it.

NOTE

This is the DBI specification that corresponds to the DBI version 1.14 ($Date: 2000/06/14 20:04:03 $).

The DBI specification is evolving at a steady pace, so it's important to check that you have the latest copy. The RECENT CHANGES section below has a summary of user-visible changes. The Changes file supplied with the DBI holds more detailed change information.

Note also that whenever the DBI changes, the drivers take some time to catch up. Recent versions of the DBI have added new features (marked NEW in the text) that may not yet be supported by the drivers you use. Talk to the authors of those drivers if you need the features.

Extensions to the DBI and other DBI related modules use the DBIx::* namespace. See Naming Conventions and Name Space and:

  http://www.perl.com/CPAN/modules/by-module/DBIx/

RECENT CHANGES

Here is a brief summary of significant user-visible changes in recent versions. (If a recent version isn't mentioned, it simply means that there were no significant user-visible changes in that version.)

Between DBI 1.00 and DBI 1.09:
  Added $dbh->selectcol_arrayref($statement) method.
  Connect now allows you to specify attribute settings within the DSN.
  For example: 
        dbi:Driver(RaiseError=>1,Taint=>1,AutoCommit=>0):dbname"
  Added $h->{Taint}, $sth->{NAME_uc}, and $sth->{NAME_lc} attributes.


DESCRIPTION

The DBI is a database access module for the Perl programming language. It defines a set of methods, 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. The DBI is a layer of ``glue'' between an application and one or more database driver modules. It is the driver modules which do most of the real work. The DBI provides a standard interface and framework for the drivers to operate within.

Architecture of a DBI Application

             |<- Scope of DBI ->|
                  .-.   .--------------.   .-------------.
  .-------.       | |---| XYZ Driver   |---| XYZ Engine  |
  | Perl  |       | |   `--------------'   `-------------'
  | script|  |A|  |D|   .--------------.   .-------------.
  | using |--|P|--|B|---|Oracle Driver |---|Oracle Engine|
  | DBI   |  |I|  |I|   `--------------'   `-------------'
  | API   |       | |...
  |methods|       | |... Other drivers
  `-------'       | |...
                  `-'

The API, or Application Programming Interface, defines the call interface and variables for Perl scripts to use. The API is implemented by the Perl DBI extension.

The DBI ``dispatches'' the method calls to the appropriate driver for actual execution. The DBI is also responsible for the dynamic loading of drivers, error checking and handling, providing default implementations for methods, and many other non-database specific duties.

Each driver contains implementations of the DBI methods using the private interface functions of the corresponding database engine. Only authors of sophisticated/multi-database applications or generic library functions need be concerned with drivers.

Notation and Conventions

The following conventions are used in this document:

  $dbh    Database handle object
  $sth    Statement handle object
  $drh    Driver handle object (rarely seen or used in applications)
  $h      Any of the handle types above ($dbh, $sth, or $drh)
  $rc     General Return Code  (boolean: true=ok, false=error)
  $rv     General Return Value (typically an integer)
  @ary    List of values returned from the database, typically a row of data
  $rows   Number of rows processed (if available, else -1)
  $fh     A filehandle
  undef   NULL values are represented by undefined values in Perl
  \%attr  Reference to a hash of attribute values passed to methods

Note that Perl will automatically destroy database and statement handle objects if all references to them are deleted.

Outline Usage

To use DBI, first you need to load the DBI module:

  use DBI;
  use strict;

(The use strict; isn't required but is strongly recommended.)

Then you need to connect to your data source and get a handle for that connection:

  $dbh = DBI->connect($dsn, $user, $password,
                      { RaiseError => 1, AutoCommit => 0 });

Since connecting can be expensive, you generally just connect at the start of your program and disconnect at the end.

Explicitly defining the required AutoCommit behavior is strongly recommended and may become mandatory in a later version. This determines whether changes are automatically committed to the database when executed, or need to be explicitly committed later.

The DBI allows an application to ``prepare'' statements for later execution. A prepared statement is identified by a statement handle held in a Perl variable. We'll call the Perl variable $sth in our examples.

The typical method call sequence for a SELECT statement is:

  prepare,
    execute, fetch, fetch, ...
    execute, fetch, fetch, ...
    execute, fetch, fetch, ...

for example:

  $sth = $dbh->prepare("SELECT foo, bar FROM table WHERE baz=?");
  $sth->execute( $baz );
  while ( @row = $sth->fetchrow_array ) {
    print "@row\n";
  }

The typical method call sequence for a non-SELECT statement is:

  prepare,
    execute,
    execute,
    execute.

for example:

  $sth = $dbh->prepare("INSERT INTO table(foo,bar,baz) VALUES (?,?,?)");
  while(<CSV>) {
    chop;
    my ($foo,$bar,$baz) = split /,/;
        $sth->execute( $foo, $bar, $baz );
  }

The do() method can be used for non repeated non-SELECT statement (or with drivers that don't support placeholders):

  $rows_affected = $dbh->do("UPDATE your_table SET foo = foo + 1");

To commit your changes to the database (when AutoCommit is off):

  $dbh->commit;  # or call $dbh->rollback; to undo changes

Finally, when you have finished working with the data source, you should disconnect from it:

  $dbh->disconnect;

General Interface Rules & Caveats

The DBI does not have a concept of a ``current session''. Every session has a handle object (i.e., a $dbh) returned from the connect method. That handle object is used to invoke database related methods.

Most data is returned to the Perl script as strings. (Null values are returned as undef.) This allows arbitrary precision numeric data to be handled without loss of accuracy. Beware 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 database engine. Time zone effects are database/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.

Most databases that understand multiple character sets have a default global charset. Text stored in the database is, or should be, stored in that charset; if not, then that's the fault of either the database or the application that inserted the data. When text is fetched it should be automatically converted to the charset of the client, presumably based on the locale. If a driver needs to set a flag to get that behavior, then it should do so; it should not require the application to do that.

Multiple SQL statements may not be combined in a single statement handle ($sth), although some databases and drivers do support this (notably Sybase and SQL Server).

Non-sequential record reads are not supported in this version of the DBI. In other words, 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 driver functions can be invoked using the DBI func() method. Private driver attributes are accessed just like standard attributes.

Many methods have an optional \%attr parameter which can be used to pass information to the driver implementing the method. Except where specifically documented, the \%attr parameter can only be used to pass driver specific hints. In general, you can ignore \%attr parameters or pass it as undef.

Naming Conventions and Name Space

The DBI package and all packages below it (DBI::*) are reserved for use by the DBI. Extensions and related modules use the DBIx:: namespace (see http://www.perl.com/CPAN/modules/by-module/DBIx/). Package names beginning with DBD:: are reserved for use by DBI database drivers. All environment variables used by the DBI or by individual DBDs begin with ``DBI_'' or ``DBD_''.

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, ISO SQL92 etc (portable)
  MixedCase     DBI API (portable), underscores are not used.
  lower_case    Driver or database engine specific (non-portable)

It is of the utmost importance that Driver developers only use lowercase attribute names when defining private attributes. Private attribute names must be prefixed with the driver name or suitable abbreviation (e.g., ``ora_'' for Oracle, ``ing_'' for Ingres, etc).

Driver Specific Prefix Registry:

  ado_     DBD::ADO
  best_    DBD::BestWins
  csv_     DBD::CSV
  db2_     DBD::DB2
  f_       DBD::File
  file_    DBD::TextFile
  ib_      DBD::InterBase
  ing_     DBD::Ingres
  ix_      DBD::Informix
  msql_    DBD::mSQL
  mysql_   DBD::mysql
  odbc_    DBD::ODBC
  ora_     DBD::Oracle
  pg_      DBD::Pg
  proxy_   DBD::Proxy
  solid_   DBD::Solid
  syb_     DBD::Sybase
  tuber_   DBD::Tuber
  xbase_   DBD::XBase

SQL - A Query Language

Most DBI drivers require applications to use a dialect of SQL (Structured Query Language) to interact with the database engine. The following links provide useful information and further links about SQL:

  http://www.altavista.com/query?q=sql+tutorial
  http://www.jcc.com/sql_stnd.html
  http://www.contrib.andrew.cmu.edu/~shadow/sql.html

The DBI itself does not mandate or require any particular language to be used; it is language independent. In ODBC terms, the DBI is in ``pass-thru'' mode, although individual drivers might not be. The only requirement is that queries and other statements must be expressed as a single string of characters passed as the first argument to the prepare or do methods.

For an interesting diversion on the real history of RDBMS and SQL, from the people who made it happen, see:

  http://ftp.digital.com/pub/DEC/SRC/technical-notes/SRC-1997-018-html/sqlr95.html

Follow the ``And the rest'' and ``Intergalactic dataspeak'' links for the SQL history.

Placeholders and Bind Values

Some drivers support placeholders and bind values. Placeholders, also called parameter markers, are used to indicate values in a database statement that will be supplied later, before the prepared statement is executed. For example, an application might use the following to insert a row of data into the SALES table:

  INSERT INTO sales (product_code, qty, price) VALUES (?, ?, ?)

or the following, to select the description for a product:

  SELECT description FROM products WHERE product_code = ?

The ? characters are the placeholders. The association of actual values with placeholders is known as binding, and the values are referred to as bind values.

When using placeholders with the SQL LIKE qualifier, you must remember that the placeholder substitutes for the whole string. So you should use ``... LIKE ? ...'' and include any wildcard characters in the value that you bind to the placeholder.

Null Values

Undefined values, or undef, can be used to indicate null values. However, care must be taken in the particular case of trying to use null values to qualify a SELECT statement. Consider:

  SELECT description FROM products WHERE product_code = ?

Binding an undef (NULL) to the placeholder will not select rows which have a NULL product_code! Refer to the SQL manual for your database engine or any SQL book for the reasons for this. To explicitly select NULLs you have to say ``WHERE product_code IS NULL'' and to make that general you have to say:

  ... WHERE (product_code = ? OR (? IS NULL AND product_code IS NULL))

and bind the same value to both placeholders.

Performance

Without using placeholders, the insert statement shown previously would have to contain the literal values to be inserted and would have to be re-prepared and re-executed for each row. With placeholders, the insert statement only needs to be prepared once. The bind values for each row can be given to the execute method each time it's called. By avoiding the need to re-prepare the statement for each row, the application typically runs many times faster. Here's an example:

  my $sth = $dbh->prepare(q{
    INSERT INTO sales (product_code, qty, price) VALUES (?, ?, ?)
  }) || die $dbh->errstr;
  while (<>) {
      chop;
      my ($product_code, $qty, $price) = split /,/;
      $sth->execute($product_code, $qty, $price) || die $dbh->errstr;
  }
  $dbh->commit || die $dbh->errstr;

See execute and bind_param for more details.

The q{...} style quoting used in this example avoids clashing with quotes that may be used in the SQL statement. Use the double-quote like qq{...} operator if you want to interpolate variables into the string. See perlop/``Quote and Quote-like Operators'' for more details.

See also the bind_column method, which is used to associate Perl variables with the output columns of a SELECT statement.


THE DBI CLASS

In this section, we cover the DBI class methods, utility functions, and the dynamic attributes associated with generic DBI handles.

DBI Class Methods

The following methods are provided by the DBI class:

connect
  $dbh = DBI->connect($data_source, $username, $password)
            || die $DBI::errstr;
  $dbh = DBI->connect($data_source, $username, $password, \%attr)
            || die $DBI::errstr;

Establishes a database connection, or session, to the requested $data_source. Returns a database handle object if the connection succeeds. Use $dbh->disconnect to terminate the connection.

If the connect fails (see below), it returns undef and sets both $DBI::err and $DBI::errstr. (It does not set $!, etc.) You should generally test the return status of connect and print $DBI::errstr if it has failed.

Multiple simultaneous connections to multiple databases through multiple drivers can be made via the DBI. Simply make one connect call for each database and keep a copy of each returned database handle.

The $data_source value should begin with ``dbi:driver_name:''. The driver_name specifies the driver that will be used to make the connection. (Letter case is significant.)

As a convenience, if the $data_source parameter is undefined or empty, the DBI will substitute the value of the environment variable DBI_DSN. If just the driver_name part is empty (i.e., the $data_source prefix is ``dbi::''), the environment variable DBI_DRIVER is used. If neither variable is set, then connect dies.

Examples of $data_source values are:

  dbi:DriverName:database_name
  dbi:DriverName:database_name@hostname:port
  dbi:DriverName:database=database_name;host=hostname;port=port

There is no standard for the text following the driver name. Each driver is free to use whatever syntax it wants. The only requirement the DBI makes is that all the information is supplied in a single string. You must consult the documentation for the drivers you are using for a description of the syntax they require. (Where a driver author needs to define a syntax for the $data_source, it is recommended that they follow the ODBC style, shown in the last example above.)

If the environment variable DBI_AUTOPROXY is defined (and the driver in $data_source is not ``Proxy'') then the connect request will automatically be changed to:

  dbi:Proxy:$ENV{DBI_AUTOPROXY};dsn=$data_source

and passed to the DBD::Proxy module. DBI_AUTOPROXY is typically set as ``hostname=...;port=...''. See the DBD::Proxy documentation for more details.

If $username or $password are undefined (rather than just empty), then the DBI will substitute the values of the DBI_USER and DBI_PASS environment variables, respectively. The DBI will warn if the environment variables are not defined. However, the everyday use of these environment variables is not recommended for security reasons. The mechanism is primarily intended to simplify testing.

DBI->connect automatically installs the driver if it has not been installed yet. Driver installation either returns a valid driver handle, or it dies with an error message that 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, in which case $DBI::errstr will hold the error message.

The $data_source argument (with the ``dbi:...:'' prefix removed) and the $username and $password arguments are then passed to the driver for processing. The DBI does not define any interpretation for the contents of these fields. The driver is free to interpret the $data_source, $username, and $password fields in any way, and supply whatever defaults are appropriate for the engine being accessed. (Oracle, for example, uses the ORACLE_SID and TWO_TASK environment variables if no $data_source is specified.)

The AutoCommit and PrintError attributes for each connection default to ``on''. (See AutoCommit and PrintError for more information.) However, it is strongly recommended that you explicitly define AutoCommit rather than rely on the default. Future versions of the DBI may issue a warning if AutoCommit is not explicitly defined.

The \%attr parameter can be used to alter the default settings of PrintError, RaiseError, AutoCommit, and other attributes. For example:

  $dbh = DBI->connect($data_source, $user, $pass, {
        PrintError => 0,
        AutoCommit => 0
  });

You can also define connection attribute values within the $data_source parameter. For example:

  dbi:DriverName(PrintError=>0,Taint=>1):...

Individual attributes values specified in this way take precedence over any conflicting values specified via the \%attr parameter to connect.

Where possible, each session ($dbh) is independent from the transactions in other sessions. This is useful when you need to hold cursors open across transactions--for example, if you use one session for your long lifespan cursors (typically read-only) and another for your short update transactions.

For compatibility with old DBI scripts, the driver can be specified by passing its name as the fourth argument to connect (instead of \%attr):

  $dbh = DBI->connect($data_source, $user, $pass, $driver);

In this ``old-style'' form of connect, the $data_source should not start with ``dbi:driver_name:''. (If it does, the embedded driver_name will be ignored). Also note that in this older form of connect, the $dbh->{AutoCommit} attribute is undefined, the $dbh->{PrintError} attribute is off, and the old DBI_DBNAME environment variable is checked if DBI_DSN is not defined. Beware that this ``old-style'' connect will be withdrawn in a future version of DBI.

connect_cached NEW
  $dbh = DBI->connect_cached($data_source, $username, $password)
            || die $DBI::errstr;
  $dbh = DBI->connect_cached($data_source, $username, $password, \%attr)
            || die $DBI::errstr;

connect_cached is like connect, except that the database handle returned is also stored in a hash associated with the given parameters. If another call is made to connect_cached with the same parameter values, then the corresponding cached $dbh will be returned if it is still valid. The cached database handle is replaced with a new connection if it has been disconnected or if the ping method fails.

Note that the behavior of this method differs in several respects from the behavior of presistent connections implemented by Apache::DBI.

Caching can be useful in some applications, but it can also cause problems and should be used with care. The exact behavior of this method is liable to change, so if you intend to use it in any production applications you should discuss your needs on the dbi-users mailing list.

The cache can be accessed (and cleared) via the CachedKids attribute.

available_drivers
  @ary = DBI->available_drivers;
  @ary = DBI->available_drivers($quiet);

Returns a list of all available drivers by searching for DBD::* modules through the directories in @INC. By default, a warning is given if some drivers are hidden by others of the same name in earlier directories. Passing a true value for $quiet will inhibit the warning.

data_sources
  @ary = DBI->data_sources($driver);
  @ary = DBI->data_sources($driver, \%attr);

Returns a list of all data sources (databases) available via the named driver. The driver will be loaded if it hasn't been already. If $driver is empty or undef, then the value of the DBI_DRIVER environment variable is used.

Data sources are returned in a form suitable for passing to the connect method (that is, they will include the ``dbi:$driver:'' prefix).

Note that many drivers have no way of knowing what data sources might be available for it. These drivers return an empty or incomplete list.

trace
  DBI->trace($trace_level)
  DBI->trace($trace_level, $trace_filename)

DBI trace information can be enabled for all handles using the trace DBI class method. To enable trace information for a specific handle, use the similar $h->trace method described elsewhere.

Trace levels are as follows:

  0 - Trace disabled.
  1 - Trace DBI method calls returning with results or errors.
  2 - Trace method entry with parameters and returning with results.
  3 - As above, adding some high-level information from the driver
      and some internal information from the DBI.
  4 - As above, adding more detailed information from the driver.
      Also includes DBI mutex information when using threaded Perl.
  5 and above - As above but with more and more obscure information.

Trace level 1 is best for a simple overview of what's happening. Trace level 2 is a good choice for general purpose tracing. Levels 3 and above (up to 9) are best reserved for investigating a specific problem, when you need to see ``inside'' the driver and DBI.

The trace output is detailed and typically very useful. Much of the trace output is formatted using the neat function, so strings in the trace output may be edited and truncated.

Initially trace output is written to STDERR. If $trace_filename is specified, then the file is opened in append mode and all trace output (including that from other handles) is redirected to that file. Further calls to trace without a $trace_filename do not alter where the trace output is sent. If $trace_filename is undefined, then trace output is sent to STDERR and the previous trace file is closed.

See also the $h->trace and $h->trace_msg methods and the DEBUGGING section for information about the DBI_TRACE environment variable.

DBI Utility Functions

In addition to the methods listed in the previous section, the DBI package also provides these 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, although internal quotes will not be escaped. Values known to be numeric will be unquoted. Undefined (NULL) values will be shown as undef (without quotes). Unprintable characters will be replaced by dot (.).

For result strings longer than $maxlen the result string will be truncated to $maxlen-4 and ``...''' will be appended. If $maxlen is 0 or undef, it defaults to $DBI::neat_maxlen which, in turn, defaults to 400.

This function is designed to format values for human consumption. It is used internally by the DBI for trace output. It should typically not be used for formatting values for database use. (See also quote.)

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 ", ".

looks_like_number
  @bool = DBI::looks_like_number(@array);

Returns true for each element that looks like a number. Returns false for each element that does not look like a number. Returns undef for each element that is undefined or empty.

DBI Dynamic Attributes

Dynamic attributes are always associated with the last handle used (that handle is represented by $h in the descriptions below).

Where an attribute is equivalent to a method call, then refer to the method call for all related documentation.

Warning: these attributes are provided as a convenience but they do have limitations. Specifically, they have a short lifespan: because they are associated with the last handle used, they should only be used immediately after calling the method that ``sets'' them. If in any doubt, use the corresponding method call.

$DBI::err
Equivalent to $h->err.

$DBI::errstr
Equivalent to $h->errstr.

$DBI::state
Equivalent to $h->state.

$DBI::rows
Equivalent to $h->rows. Please refer to the documentation for the rows method.


METHODS COMMON TO ALL HANDLES

The following methods can be used by all types of DBI handles.

err
  $rv = $h->err;

Returns the native database engine error code from the last driver method called. The code is typically an integer but you should not assume that.

The DBI resets $h->err to undef before most DBI method calls, so the value only has a short lifespan. Also, most drivers share the same error variables across all their handles, so calling a method on one handle will typically reset the error on all the other handles that are children of that driver.

If you need to test for individual errors and have your program be portable to different database engines, then you'll need to determine what the corresponding error codes are for all those engines and test for all of them.

errstr
  $str = $h->errstr;

Returns the native database engine error message from the last driver method called. This has the same lifespan issues as the err method described above.

state
  $str = $h->state;

Returns an error code in the standard SQLSTATE five character format. Note that the specific success code 00000 is translated to 0 (false). If the driver does not support SQLSTATE (and most don't), then state will return S1000 (General Error) for all errors.

trace
  $h->trace($trace_level);
  $h->trace($trace_level, $trace_filename);

DBI trace information can be enabled for a specific handle (and any future children of that handle) by setting the trace level using the trace method.

Trace level 1 is best for a simple overview of what's happening. Trace level 2 is a good choice for general purpose tracing. Levels 3 and above (up to 9) are best reserved for investigating a specific problem, when you need to see ``inside'' the driver and DBI. Set $trace_level to 0 to disable the trace.

The trace output is detailed and typically very useful. Much of the trace output is formatted using the neat function, so strings in the trace output may be edited and truncated.

Initially, trace output is written to STDERR. If $trace_filename is specified, then the file is opened in append mode and all trace output (including that from other handles) is redirected to that file. Further calls to trace without a $trace_filename do not alter where the trace output is sent. If $trace_filename is undefined, then trace output is sent to STDERR and the previous trace file is closed.

See also the DBI->trace method and DEBUGGING for information about the DBI_TRACE environment variable.

trace_msg
  $h->trace_msg($message_text);
  $h->trace_msg($message_text, $min_level);

Writes $message_text to the trace file if trace is enabled for $h or for the DBI as a whole. Can also be called as DBI->trace_msg($msg). See trace.

If $min_level is defined, then the message is output only if the trace level is equal to or greater than that level. $min_level defaults to 1.

func
  $h->func(@func_arguments, $func_name);

The func method can be used to call private non-standard and non-portable methods implemented by the driver. Note that the function name is given as the last argument.

This method is not directly related to calling stored procedures. Calling stored procedures is currently not defined by the DBI. Some drivers, such as DBD::Oracle, support it in non-portable ways. See driver documentation for more details.


ATTRIBUTES COMMON TO ALL HANDLES

These attributes are common to all types of DBI handles.

Some attributes are inherited by child handles. That is, the value of an inherited attribute in a newly created statement handle is the same as the value in the parent database handle. Changes to attributes in the new statement handle do not affect the parent database handle and changes to the database handle do not affect existing statement handles, only future ones.

Attempting to set or get the value of an unknown attribute is fatal, except for private driver specific attributes (which all have names starting with a lowercase letter).

Example:

  $h->{AttributeName} = ...;    # set/write
  ... = $h->{AttributeName};    # get/read
Warn (boolean, inherited)
Enables useful warnings for certain bad practices. Enabled by default. Some emulation layers, especially those for Perl 4 interfaces, disable warnings. Since warnings are generated using the Perl warn function, they can be intercepted using the Perl $SIG{__WARN__} hook.

Active (boolean, read-only)
True if the handle object is ``active''. This is rarely used in applications. The exact meaning of active is somewhat vague at the moment. For a database handle it typically means that the handle is connected to a database ($dbh->disconnect sets Active off). For a statement handle it typically means that the handle is a SELECT that may have more data to fetch. (Fetching all the data or calling $sth->finish sets Active off.)

Kids (integer, read-only)
For a driver handle, Kids is the number of currently existing database handles that were created from that driver handle. For a database handle, Kids is the number of currently existing statement handles that were created from that database handle.

ActiveKids (integer, read-only)
Like Kids, but only counting those that are Active (as above).

CachedKids (hash ref)
For a database handle, returns a reference to the cache (hash) of statement handles created by the prepare_cached method. For a driver handle, returns a reference to the cache (hash) of database handles created by the connect_cached method.

CompatMode (boolean, inherited)
Used by emulation layers (such as Oraperl) to enable compatible behavior in the underlying driver (e.g., DBD::Oracle) for this handle. Not normally set by application code.

InactiveDestroy (boolean)
This attribute can be used to disable the database engine related effect of DESTROYing a handle (which would normally close a prepared statement or disconnect from the database etc).

For a database handle, this attribute does not disable an explicit call to the disconnect method, only the implicit call from DESTROY.

This attribute is specifically designed for use in Unix applications that ``fork'' child processes. Either the parent or the child process, but not both, should set InactiveDestroy on all their shared handles. Note that some databases, including Oracle, don't support passing a database connection across a fork.

PrintError (boolean, inherited)
This attribute can be used to force errors to generate warnings (using warn) in addition to returning error codes in the normal way. When set ``on'', any method which results in an error occuring will cause the DBI to effectively do a warn("$class $method failed: $DBI::errstr") where $class is the driver class and $method is the name of the method which failed. E.g.,
  DBD::Oracle::db prepare failed: ... error text here ...

By default, DBI->connect sets PrintError ``on''.

If desired, the warnings can be caught and processed using a $SIG{__WARN__} handler or modules like CGI::Carp and CGI::ErrorWrap.

RaiseError (boolean, inherited)
This attribute can be used to force errors to raise exceptions rather than simply return error codes in the normal way. It is ``off'' by default. When set ``on'', any method which results in an error will cause the DBI to effectively do a die("$class $method failed: $DBI::errstr"), where $class is the driver class and $method is the name of the method that failed. E.g.,
  DBD::Oracle::db prepare failed: ... error text here ...

If PrintError is also on, then the PrintError is done before the RaiseError unless no __DIE__ handler has been defined, in which case PrintError is skipped since the die will print the message.

If you want to temporarily turn RaiseError off (inside a library function that is likely to fail, for example), the recommended way is like this:

  {
    local $h->{RaiseError};  # localize and turn off for this block
    ...
  }

The original value will automatically and reliably be restored by Perl, regardless of how the block is exited. The same logic applies to other attributes, including PrintError.

Sadly, this doesn't work for Perl versions up to and including 5.004_04. For backwards compatibility, you could just use eval { ... } instead.

ChopBlanks (boolean, inherited)
This attribute can be used to control the trimming of trailing space characters from fixed width character (CHAR) fields. No other field types are affected, even where field values have trailing spaces.

The default is false (although it is possible that the default may change). Applications that need specific behavior should set the attribute as needed. Emulation interfaces should set the attribute to match the behavior of the interface they are emulating.

Drivers are not required to support this attribute, but any driver which does not support it must arrange to return undef as the attribute value.

LongReadLen (unsigned integer, inherited)
This attribute may be used to control the maximum length of long fields (``blob'', ``memo'', etc.) which the driver will read from the database automatically when it fetches each row of data. The LongReadLen attribute only relates to fetching and reading long values; it is not involved in inserting or updating them.

A value of 0 means not to automatically fetch any long data. (fetch should return undef for long fields when LongReadLen is 0.)

The default is typically 0 (zero) bytes but may vary between drivers. Applications fetching long fields should set this value to slightly larger than the longest long field value to be fetched.

Some databases return some long types encoded as pairs of hex digits. For these types, LongReadLen relates to the underlying data length and not the doubled-up length of the encoded string.

Changing the value of LongReadLen for a statement handle after it has been prepare'd will typically have no effect, so it's common to set LongReadLen on the $dbh before calling prepare.

Note that the value used here has a direct effect on the memory used by the application, so don't be too generous.

See LongTruncOk for more information on truncation behavior.

LongTruncOk (boolean, inherited)
This attribute may be used to control the effect of fetching a long field value which has been truncated (typically because it's longer than the value of the LongReadLen attribute).

By default, LongTruncOk is false and so fetching a long value that needs to be truncated will cause the fetch to fail. (Applications should always be sure to check for errors after a fetch loop in case an error, such as a divide by zero or long field truncation, caused the fetch to terminate prematurely.)

If a fetch fails due to a long field truncation when LongTruncOk is false, many drivers will allow you to continue fetching further rows.

See also LongReadLen.

Taint (boolean, inherited)
If this attribute is set to a true value and Perl is running in taint mode (e.g., started with the -T option), then all data fetched from the database is tainted, and the arguments to most DBI method calls are checked for being tainted. This may change.

The attribute defaults to off, even if Perl is in taint mode. See perlsec for more about taint mode. If Perl is not running in taint mode, this attribute has no effect.

When fetching data that you trust you can turn off the Taint attribute, for that statement handle, for the duration of the fetch loop.

Currently only fetched data is tainted. It is possible that the results of other DBI method calls, and the value of fetched attributes, may also be tainted in future versions. That change may well break your applications unless you take great care now. If you use DBI Taint mode, please report your experience and any suggestions for changes.

private_*
The DBI provides a way to store extra information in a DBI handle as ``private'' attributes. The DBI will allow you to store and retreive any attribute which has a name starting with ``private_''. It is strongly recommended that you use just one private attribute (e.g., use a hash ref) and give it a long and unambiguous name that includes the module or application name that the attribute relates to (e.g., ``private_YourFullModuleName_thingy'').


DBI DATABASE HANDLE OBJECTS

This section covers the methods and attributes associated with database handles.

Database Handle Methods

The following methods are specified for DBI database handles:

do
  $rc  = $dbh->do($statement)           || die $dbh->errstr;
  $rc  = $dbh->do($statement, \%attr)   || die $dbh->errstr;
  $rv  = $dbh->do($statement, \%attr, @bind_values) || ...

Prepare and execute a single statement. Returns the number of rows affected or undef on error. A return value of -1 means the number of rows is not known or is not available.

This method is typically most useful for non-SELECT statements that either cannot be prepared in advance (due to a limitation of the driver) or do not need to be executed repeatedly. It should not be used for SELECT statements because it does not return a statement handle (so you can't fetch any data).

The default do method is logically similar to:

  sub do {
      my($dbh, $statement, $attr, @bind_values) = @_;
      my $sth = $dbh->prepare($statement, $attr) or return undef;
      $sth->execute(@bind_values) or return undef;
      my $rows = $sth->rows;
      ($rows == 0) ? "0E0" : $rows; # always return true if no error
  }

For example:

  my $rows_deleted = $dbh->do(q{
      DELETE FROM table
      WHERE status = ?
  }, undef, 'DONE') || die $dbh->errstr;

Using placeholders and @bind_values with the do method can be useful because it avoids the need to correctly quote any variables in the $statement. But if you'll be executing the statement many times then it's more efficient to prepare it once and call execute many times instead.

The q{...} style quoting used in this example avoids clashing with quotes that may be used in the SQL statement. Use the double-quote-like qq{...} operator if you want to interpolate variables into the string. See perlop/``Quote and Quote-like Operators'' for more details.

selectrow_array
  @row_ary = $dbh->selectrow_array($statement);
  @row_ary = $dbh->selectrow_array($statement, \%attr);
  @row_ary = $dbh->selectrow_array($statement, \%attr, @bind_values);

This utility method combines prepare, execute and fetchrow_array into a single call. If called in a list context, it returns the first row of data from the statement. If called in a scalar context, it returns the first field of the first row. The $statement parameter can be a previously prepared statement handle, in which case the prepare is skipped.

If any method fails, and RaiseError is not set, selectrow_array will return an empty list.

In a scalar context, selectrow_array returns the value of the first field. An undef is returned if there are no matching rows or an error occurred. Since that undef can't be distinguished from an undef returned because the first field value was NULL, calling selectrow_array in a scalar context should be used with caution.

selectall_arrayref
  $ary_ref = $dbh->selectall_arrayref($statement);
  $ary_ref = $dbh->selectall_arrayref($statement, \%attr);
  $ary_ref = $dbh->selectall_arrayref($statement, \%attr, @bind_values);

This utility method combines prepare, execute and fetchall_arrayref into a single call. It returns a reference to an array containing a reference to an array for each row of data fetched.

The $statement parameter can be a previously prepared statement handle, in which case the prepare is skipped. This is recommended if the statement is going to be executed many times.

If any method except fetch fails, and RaiseError is not set, selectall_arrayref will return undef. If fetch fails and RaiseError is not set, then it will return with whatever data it has fetched thus far.

selectcol_arrayref
  $ary_ref = $dbh->selectcol_arrayref($statement);
  $ary_ref = $dbh->selectcol_arrayref($statement, \%attr);
  $ary_ref = $dbh->selectcol_arrayref($statement, \%attr, @bind_values);

This utility method combines prepare, execute, and fetching one column from all the rows, into a single call. It returns a reference to an array containing the values of the first column from each row.

The $statement parameter can be a previously prepared statement handle, in which case the prepare is skipped. This is recommended if the statement is going to be executed many times.

If any method except fetch fails, and RaiseError is not set, selectall_arrayref will return undef. If fetch fails and RaiseError is not set, then it will return with whatever data it has fetched thus far.

prepare
  $sth = $dbh->prepare($statement)          || die $dbh->errstr;
  $sth = $dbh->prepare($statement, \%attr)  || die $dbh->errstr;

Prepares a single statement for later execution by the database engine and returns a reference to a statement handle object.

The returned statement handle can be used to get attributes of the statement and invoke the execute method. See Statement Handle Methods.

Drivers for engines without 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 unlikely to give much useful information about the statement, such as $sth->{NUM_OF_FIELDS}, until after $sth->execute has been called. Portable applications should take this into account.

In general, DBI drivers do not parse the contents of the statement (other than simply counting any Placeholders). The statement is passed directly to the database engine, sometimes known as pass-thru mode. This has advantages and disadvantages. On the plus side, you can access all the functionality of the engine being used. On the downside, you're limited if you're using a simple engine, and you need to take extra care if writing applications intended to be portable between engines.

Portable applications should not assume that a new statement can be prepared and/or executed while still fetching results from a previous statement.

Some command-line SQL tools use statement terminators, like a semicolon, to indicate the end of a statement. Such terminators should not normally be used with the DBI.

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. If the cached $sth being returned is active (i.e., is a SELECT that may still have data to be fetched) then a warning will be generated and finish will be called for you. The 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);
commit
  $rc  = $dbh->commit     || die $dbh->errstr;

Commit (make permanent) the most recent series of database changes if the database supports transactions and AutoCommit is off.

If AutoCommit is on, then calling commit will issue a ``commit ineffective with AutoCommit'' warning.

See also Transactions in the FURTHER INFORMATION section below.

rollback
  $rc  = $dbh->rollback   || die $dbh->errstr;

Rollback (undo) the most recent series of uncommitted database changes if the database supports transactions and AutoCommit is off.

If AutoCommit is on, then calling rollback will issue a ``rollback ineffective with AutoCommit'' warning.

See also Transactions in the FURTHER INFORMATION section below.

disconnect
  $rc = $dbh->disconnect  || warn $dbh->errstr;

Disconnects the database from the database handle. disconnect is typically only used before exiting the program. The handle is of little use after disconnecting.

The transaction behavior of the disconnect method is, sadly, undefined. Some database systems (such as Oracle and Ingres) will automatically commit any outstanding changes, but others (such as Informix) will rollback any outstanding changes. Applications not using AutoCommit 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 implicitly call rollback to undo any uncommitted changes. This is vital behavior to ensure that incomplete transactions don't get committed simply because Perl calls DESTROY on every object before exiting. Also, do not rely on the order of object destruction during ``global destruction'', as it is undefined.

Generally, if you want your changes to be commited or rolled back when you disconnect, then you should explicitly call commit or rollback before disconnecting.

If you disconnect from a database while you still have active statement handles, you will get a warning. The statement handles should either be cleared (destroyed) before disconnecting, or the finish method should be called on each one.

ping
  $rc = $dbh->ping;

Attempts to determine, in a reasonably efficient way, if the database server is still running and the connection to it is still working. Individual drivers should implement this function in the most suitable manner for their database engine.

The current default implementation always returns true without actually doing anything. Actually, it returns ``0 but true'' which is true but zero. That way you can tell if the return value is genuine or just the default. Drivers should override this method with one that does the right thing for their type of database.

Few applications would have use for this method. See the specialized Apache::DBI module for one example usage.

table_info NEW
Warning: This method is experimental and may change.
  $sth = $dbh->table_info;

Returns an active statement handle that can be used to fetch information about tables and views that exist in the database.

The handle has at least the following fields in the order show below. Other fields, after these, may also be present.

TABLE_CAT: Table catalog identifier. This field is NULL (undef) if not applicable to the data source, which is usually the case. This field is empty if not applicable to the table.

TABLE_SCHEM: The name of the schema containing the TABLE_NAME value. This field is NULL (undef) if not applicable to data source, and empty if not applicable to the table.

TABLE_NAME: Name of the table (or view, synonym, etc).

TABLE_TYPE: One of the following: ``TABLE'', ``VIEW'', ``SYSTEM TABLE'', ``GLOBAL TEMPORARY'', ``LOCAL TEMPORARY'', ``ALIAS'', ``SYNONYM'' or a type identifier that is specific to the data source.

REMARKS: A description of the table. May be NULL (undef).

Note that table_info might not return records for all tables. Applications can use any valid table regardless of whether it's returned by table_info. See also tables.

For more detailed information about the fields and their meanings, you can refer to:

  http://msdn.microsoft.com/library/psdk/dasdk/odch6wqb.htm

If that URL ceases to work then use the MSDN search facility at:

  http://search.microsoft.com/us/dev/

and search for SQLTables returns using the exact phrase option. The link you want will probably just be called SQLTables and will be part of the Data Access SDK.

tables NEW
Warning: This method is experimental and may change.
  @names = $dbh->tables;

Returns a list of table and view names, possibly including a schema prefix. This list should include all tables that can be used in a SELECT statement without further qualification.

Note that table_info might not return records for all tables. Applications can use any valid table regardless of whether it's returned by tables. See also table_info.

type_info_all NEW
Warning: This method is experimental and may change.
  $type_info_all = $dbh->type_info_all;

Returns a reference to an array which holds information about each data type variant supported by the database and driver. The array and its contents should be treated as read-only.

The first item is a reference to a hash of Name => Index pairs. The following items are references to arrays, one per supported data type variant. The leading hash defines the names and order of the fields within the following list of arrays. For example:

  $type_info_all = [
    {   TYPE_NAME         => 0,
        DATA_TYPE         => 1,
        COLUMN_SIZE       => 2,     # was PRECISION originally
        LITERAL_PREFIX    => 3,
        LITERAL_SUFFIX    => 4,
        CREATE_PARAMS     => 5,
        NULLABLE          => 6,
        CASE_SENSITIVE    => 7,
        SEARCHABLE        => 8,
        UNSIGNED_ATTRIBUTE=> 9,
        FIXED_PREC_SCALE  => 10,    # was MONEY originally
        AUTO_UNIQUE_VALUE => 11,    # was AUTO_INCREMENT originally
        LOCAL_TYPE_NAME   => 12,
        MINIMUM_SCALE     => 13,
        MAXIMUM_SCALE     => 14,
        NUM_PREC_RADIX    => 15,
    },
    [ 'VARCHAR', SQL_VARCHAR,
        undef, "'","'", undef,0, 1,1,0,0,0,undef,1,255, undef
    ],
    [ 'INTEGER', SQL_INTEGER,
        undef,  "", "", undef,0, 0,1,0,0,0,undef,0,  0, 10
    ],
  ];

Note that more than one row may have the same value in the DATA_TYPE field if there are different ways to spell the type name and/or there are variants of the type with different attributes (e.g., with and without AUTO_UNIQUE_VALUE set, with and without UNSIGNED_ATTRIBUTE, etc).

The rows are ordered by DATA_TYPE first and then by how closely each type maps to the corresponding ODBC SQL data type, closest first.

The meaning of the fields is described in the documentation for the type_info method. The index values shown above (e.g., NULLABLE => 6) are for illustration only. Drivers may define the fields with a different order.

This method is not normally used directly. The type_info method provides a more useful interface to the data.

type_info NEW
Warning: This method is experimental and may change.
  @type_info = $dbh->type_info($data_type);

Returns a list of hash references holding information about one or more variants of $data_type. The list is ordered by DATA_TYPE first and then by how closely each type maps to the corresponding ODBC SQL data type, closest first. If called in a scalar context then only the first (best) element is returned.

If $data_type is undefined or SQL_ALL_TYPES, then the list will contain hashes for all data type variants supported by the database and driver.

If $data_type is an array reference then type_info returns the information for the first type in the array that has any matches.

The keys of the hash follow the same letter case conventions as the rest of the DBI (see Naming Conventions and Name Space). The following items should exist:

TYPE_NAME (string)
Data type name for use in CREATE TABLE statements etc.

DATA_TYPE (integer)
SQL data type number.

COLUMN_SIZE (integer)
For numeric types, this is either the total number of digits (if the NUM_PREC_RADIX value is 10) or the total number of bits allowed in the column (if NUM_PREC_RADIX is 2).

For string types, this is the maximum size of the string in bytes.

For date and interval types, this is the maximum number of characters needed to display the value.

LITERAL_PREFIX (string)
Characters used to prefix a literal. A typical prefix is ``''' for characters, or possibly ``0x'' for binary values passed as hexadecimal. NULL (undef) is returned for data types for which this is not applicable.

LITERAL_SUFFIX (string)
Characters used to suffix a literal. Typically ``''' for characters. NULL (undef) is returned for data types where this is not applicable.

CREATE_PARAMS (string)
Parameter names for data type definition. For example, CREATE_PARAMS for a DECIMAL would be ``precision,scale'' if the DECIMAL type should be declared as DECIMAL(precision,scale) where precision and scale are integer values. For a VARCHAR it would be ``max length''. NULL (undef) is returned for data types for which this is not applicable.

NULLABLE (integer)
Indicates whether the data type accepts a NULL value: 0 or an empty string = no, 1 = yes, 2 = unknown.

CASE_SENSITIVE (boolean)
Indicates whether the data type is case sensitive in collations and comparisons.

SEARCHABLE (integer)
Indicates how the data type can be used in a WHERE clause, as follows:
  0 - Cannot be used in a WHERE clause
  1 - Only with a LIKE predicate
  2 - All comparison operators except LIKE
  3 - Can be used in a WHERE clause with any comparison operator

UNSIGNED_ATTRIBUTE (boolean)
Indicates whether the data type is unsigned. NULL (undef) is returned for data types for which this is not applicable.

FIXED_PREC_SCALE (boolean)
Indicates whether the data type always has the same precision and scale (such as a money type). NULL (undef) is returned for data types for which this is not applicable.

AUTO_UNIQUE_VALUE (boolean)
Indicates whether a column of this data type is automatically set to a unique value whenever a new row is inserted. NULL (undef) is returned for data types for which this is not applicable.

LOCAL_TYPE_NAME (string)
Localized version of the TYPE_NAME for use in dialog with users. NULL (undef) is returned if a localized name is not available (in which case TYPE_NAME should be used).

MINIMUM_SCALE (integer)
The minimum scale of the data type. If a data type has a fixed scale, then MAXIMUM_SCALE holds the same value. NULL (undef) is returned for data types for which this is not applicable.

MAXIMUM_SCALE (integer)
The maximum scale of the data type. If a data type has a fixed scale, then MINIMUM_SCALE holds the same value. NULL (undef) is returned for data types for which this is not applicable.

SQL_DATA_TYPE (integer)
This column is the same as the DATA_TYPE column, except for interval and datetime data types. For interval and datetime data types, the SQL_DATA_TYPE field will return SQL_INTERVAL or SQL_DATETIME, and the SQL_DATETIME_SUB field below will return the subcode for the specific interval or datetime data type. If this field is NULL, then the driver does not support or report on interval or date subtypes.

SQL_DATETIME_SUB (integer)
For interval or datetime data types, where the SQL_DATA_TYPE field above is SQL_INTERVAL or SQL_DATETIME, this field will hold the subcode for the specific interval or datetime data type. Otherwise it will be NULL (undef).

NUM_PREC_RADIX (integer)
The radix value of the data type. For approximate numeric types, NUM_PREC_RADIX contains the value 2 and COLUMN_SIZE holds the number of bits. For exact numeric types, NUM_PREC_RADIX contains the value 10 and COLUMN_SIZE holds the number of decimal digits. NULL (undef) is returned either for data types for which this is not applicable or if the driver cannot report this information.

INTERVAL_PRECISION (integer)
The interval leading precision for interval types. NULL is returned either for data types for which this is not applicable or if the driver cannot report this information.

Since DBI and ODBC drivers vary in how they map their types into the ISO standard types you may need to search for more than one type. Here's an example looking for a usable type to store a date:

  $my_date_type = $dbh->type_info( [ SQL_DATE, SQL_TIMESTAMP ] );

Similarly, to more reliably find a type to store small integers, you could use a list starting with SQL_SMALLINT, SQL_INTEGER, SQL_DECIMAL, etc.

For more detailed information about these fields and their meanings, you can refer to:

  http://msdn.microsoft.com/library/psdk/dasdk/odch6yy7.htm

If that URL ceases to work then use the MSDN search facility at


    http://search.microsoft.com/us/dev/

and search the MSDN Library for C<SQLGetTypeInfo returns> using the exact phrase option.
The link you want will probably just be called C<SQLGetTypeInfo> (there
may be more than one).

The individual data types are currently described here:

  http://msdn.microsoft.com/library/psdk/dasdk/odap8fcj.htm

If that URL ceases to work, or to get more general information, use the MSDN search facility as described above and search for SQL Data Types.

quote
  $sql = $dbh->quote($value);
  $sql = $dbh->quote($value, $data_type);

Quote a string literal for use as a literal value 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");

For most database types, quote would return 'Don''t' (including the outer quotation marks).

An undefined $value value will be returned as the string NULL (without quotation marks) to match how NULLs are represented in SQL.

If $data_type is supplied, it is used to try to determine the required quoting behavior by using the information returned by type_info. As a special case, the standard numeric types are optimized to return $value without calling type_info.

Quote will probably not be able to deal with all possible input (such as binary data or data containing newlines), and is not related in any way with escaping or quoting shell meta-characters. There is no need to quote values being used with Placeholders and Bind Values.

Database Handle Attributes

This section describes attributes specific to database handles.

Changes to these database handle attributes do not affect any other existing or future database handles.

Attempting to set or get the value of an unknown attribute is fatal, except for private driver-specific attributes (which all have names starting with a lowercase letter).

Example:

  $h->{AutoCommit} = ...;       # set/write
  ... = $h->{AutoCommit};       # get/read
AutoCommit (boolean)
If true, then database changes cannot be rolled-back (undone). If false, then database changes automatically occur within a ``transaction'', which must either be committed or rolled back using the commit or rollback methods.

Drivers should always default to AutoCommit mode (an unfortunate choice largely forced on the DBI by ODBC and JDBC conventions.)

Attempting to set AutoCommit to an unsupported value is a fatal error. This is an important feature of the DBI. Applications that need full transaction behavior can set $dbh->{AutoCommit} = 0 (or set AutoCommit to 0 via connect) without having to check that the value was assigned successfully.

For the purposes of this description, we can divide databases into three categories:

  Databases which don't support transactions at all.
  Databases in which a transaction is always active.
  Databases in which a transaction must be explicitly started (C<'BEGIN WORK'>).

* Databases which don't support transactions at all

For these databases, attempting to turn AutoCommit off is a fatal error. commit and rollback both issue warnings about being ineffective while AutoCommit is in effect.

* Databases in which a transaction is always active

These are typically mainstream commercial relational databases with ``ANSI standard'' transaction behavior. If AutoCommit is off, then changes to the database won't have any lasting effect unless commit is called (but see also disconnect). If rollback is called then any changes since the last commit are undone.

If AutoCommit is on, then the effect is the same as if the DBI called commit automatically after every successful database operation. In other words, calling commit or rollback explicitly while AutoCommit is on would be ineffective because the changes would have already been commited.

Changing AutoCommit from off to on should issue a commit in most drivers.

Changing AutoCommit from on to off should have no immediate effect.

For databases which don't support a specific auto-commit mode, the driver has to commit each statement automatically using an explicit COMMIT after it completes successfully (and roll it back using an explicit ROLLBACK if it fails). The error information reported to the application will correspond to the statement which was executed, unless it succeeded and the commit or rollback failed.

* Databases in which a transaction must be explicitly started

For these databases, the intention is to have them act like databases in which a transaction is always active (as described above).

To do this, the DBI driver will automatically begin a transaction when AutoCommit is turned off (from the default ``on'' state) and will automatically begin another transaction after a commit or rollback. In this way, the application does not have to treat these databases as a special case.

See disconnect for other important notes about transactions.

Driver (handle)
Holds the handle of the parent driver. The only recommended use for this is to find the name of the driver using:
  $dbh->{Driver}->{Name}

Name (string)
Holds the ``name'' of the database. Usually (and recommended to be) the same as the ``dbi:DriverName:...'' string used to connect to the database, but with the leading ``dbi:DriverName:'' removed.

RowCacheSize (integer) NEW
A hint to the driver indicating the size of the local row cache that the application would like the driver to use for future SELECT statements. If a row cache is not implemented, then setting RowCacheSize is ignored and getting the value returns undef.

Some RowCacheSize values have special meaning, as follows:

  0 - Automatically determine a reasonable cache size for each C<SELECT>
  1 - Disable the local row cache
 >1 - Cache this many rows
 <0 - Cache as many rows that will fit into this much memory for each C<SELECT>.

Note that large cache sizes may require a very large amount of memory (cached rows * maximum size of row). Also, a large cache will cause a longer delay not only for the first fetch, but also whenever the cache needs refilling.

See also the RowsInCache statement handle attribute.


DBI STATEMENT HANDLE OBJECTS

This section lists the methods and attributes associated with DBI statement handles.

Statement Handle Methods

The DBI defines the following methods for use on DBI statement handles:

bind_param
  $rc = $sth->bind_param($p_num, $bind_value)  || die $sth->errstr;
  $rv = $sth->bind_param($p_num, $bind_value, \%attr)     || ...
  $rv = $sth->bind_param($p_num, $bind_value, $bind_type) || ...

The bind_param method can be used to bind a value with a placeholder embedded in the prepared statement. Placeholders are indicated with question mark character (?). For example:

  $dbh->{RaiseError} = 1;        # save having to check each method call
  $sth = $dbh->prepare("SELECT name, age FROM people WHERE name LIKE ?");
  $sth->bind_param(1, "John%");  # placeholders are numbered from 1
  $sth->execute;
  DBI::dump_results($sth);

Note that the ? is not enclosed in quotation marks, even when the placeholder represents a string. Some drivers also allow placeholders like :name and :n (e.g., :1, :2, and so on) in addition to ?, but their use is not portable. Undefined bind values or undef can be used to indicate null values.

Some drivers do not support placeholders.

With most drivers, placeholders can't be used for any element of a statement that would prevent the database server from validating the statement and creating a query execution plan for it. For example:

  "SELECT name, age FROM ?"         # wrong (will probably fail)
  "SELECT name, ?   FROM people"    # wrong (but may not 'fail')

Also, placeholders can only represent single scalar values. For example, the following statement won't work as expected for more than one value:

  "SELECT name, age FROM people WHERE name IN (?)"    # wrong

Data Types for Placeholders

The \%attr parameter can be used to hint at the data type the placeholder should have. Typically, the driver is only interested in knowing if the placeholder should be bound as a number or a string.

  $sth->bind_param(1, $value, { TYPE => SQL_INTEGER });

As a short-cut for this common case, the data type can be passed directly, in place of the \%attr hash reference. This example is equivalent to the one above:

  $sth->bind_param(1, $value, SQL_INTEGER);

The TYPE value indicates the standard (non-driver-specific) type for this parameter. To specify the driver-specific type, the driver may support a driver-specific attribute, such as { ora_type => 97 }. The data type for a placeholder cannot be changed after the first bind_param call. However, it can be left unspecified, in which case it defaults to the previous value.

Perl only has string and number scalar data types. All database types that aren't numbers are bound as strings and must be in a format the database will understand.

As an alternative to specifying the data type in the bind_param call, you can let the driver pass the value as the default type (VARCHAR). You can then use an SQL function to convert the type within the statement. For example:

  INSERT INTO price(code, price) VALUES (?, CONVERT(MONEY,?))

The CONVERT function used here is just an example. The actual function and syntax will vary between different databases and is non-portable.

See also Placeholders and Bind Values for more information.

bind_param_inout
  $rc = $sth->bind_param_inout($p_num, \$bind_value, $max_len)  || die $sth->errstr;
  $rv = $sth->bind_param_inout($p_num, \$bind_value, $max_len, \%attr)     || ...
  $rv = $sth->bind_param_inout($p_num, \$bind_value, $max_len, $bind_type) || ...

This method acts like bind_param, but also enables values to be updated by the statement. The statement is typically a call to a stored procedure. The $bind_value must be passed as a reference to the actual value to be used.

Note that unlike bind_param, the $bind_value variable is not read when bind_param_inout is called. Instead, the value in the variable is read at the time execute is called.

The additional $max_len parameter specifies the minimum amount of memory to allocate to $bind_value for the new value. If the value returned from the database is too big to fit, then the execution should fail. If unsure what value to use, pick a generous length, i.e., a length larger than the longest value that would ever be returned. The only cost of using a larger value than needed is wasted memory.

It is expected that few drivers will support this method. The only driver currently known to do so is DBD::Oracle (DBD::ODBC may support it in a future release). Therefore it should not be used for database independent applications.

Undefined values or undef are used to indicate null values. See also Placeholders and Bind Values for more information.

execute
  $rv = $sth->execute                || die $sth->errstr;
  $rv = $sth->execute(@bind_values)  || die $sth->errstr;

Perform whatever processing is necessary to execute the prepared statement. An undef is returned if an error occurs. A successful execute always returns true regardless of the number of rows affected, even if it's zero (see below). It is always important to check the return status of execute (and most other DBI methods) for errors.

For a non-SELECT statement, execute returns the number of rows affected, if known. If no rows were affected, then execute returns ``0E0'', which Perl will treat as 0 but will regard as true. Note that it is not an error for no rows to be affected by a statement. If the number of rows affected is not known, then execute returns -1.

For SELECT statements, execute simply ``starts'' the query within the database engine. Use one of the fetch methods to retreive the data after calling execute. The execute method does not return the number of rows that will be returned by the query (because most databases can't tell in advance), it simply returns a true value.

If any arguments are given, then execute will effectively call bind_param for each value before executing the statement. Values bound in this way are usually treated as SQL_VARCHAR types unless the driver can determine the correct type (which is rare), or unless bind_param (or bind_param_inout) has already been used to specify the type.

fetchrow_arrayref
  $ary_ref = $sth->fetchrow_arrayref;
  $ary_ref = $sth->fetch;    # alias

Fetches the next row of data and returns a reference to an array holding the field values. Null fields are returned as undef values in the array. This is the fastest way to fetch data, particularly if used with $sth->bind_columns.

If there are no more rows or if an error occurs, then fetchrow_arrayref returns an undef. You should check $sth->err afterwards (or use the RaiseError attribute) to discover if the undef returned was due to an error.

Note that the same array reference will currently be returned for each fetch, so don't store the reference and then use it after a later fetch.

fetchrow_array
 @ary = $sth->fetchrow_array;

An alternative to fetchrow_arrayref. Fetches the next row of data and returns it as a list containing the field values. Null fields are returned as undef values in the list.

If there are no more rows or if an error occurs, then fetchrow_array returns an empty list. You should check $sth->err afterwards (or use the RaiseError attribute) to discover if the empty list returned was due to an error.

In a scalar context, fetchrow_array returns the value of the first field. An undef is returned if there are no more rows or if an error occurred. Since that undef can't be distinguished from an undef returned because the first field value was NULL, you should exercise some caution if you use fetchrow_array in a scalar context.

fetchrow_hashref
 $hash_ref = $sth->fetchrow_hashref;
 $hash_ref = $sth->fetchrow_hashref($name);

An alternative to fetchrow_arrayref. Fetches the next row of data and returns it as a reference to a hash containing field name and field value pairs. Null fields are returned as undef values in the hash.

If there are no more rows or if an error occurs, then fetchrow_hashref returns an undef. You should check $sth->err afterwards (or use the RaiseError attribute) to discover if the undef returned was due to an error.

The optional $name parameter specifies the name of the statement handle attribute. For historical reasons it defaults to ``NAME'', however using either ``NAME_lc'' or ``NAME_uc'' is recomended for portability.

The keys of the hash are the same names returned by $sth->{$name}. If more than one field has the same name, there will only be one entry in the returned hash for those fields.

Because of the extra work fetchrow_hashref and Perl have to perform, it is not as efficient as fetchrow_arrayref or fetchrow_array.

Currently, a new hash reference is returned for each row. This will change in the future to return the same hash ref each time, so don't rely on the current behaviour.

fetchall_arrayref
  $tbl_ary_ref = $sth->fetchall_arrayref;
  $tbl_ary_ref = $sth->fetchall_arrayref( $slice_array_ref );
  $tbl_ary_ref = $sth->fetchall_arrayref( $slice_hash_ref  );

The fetchall_arrayref method can be used to fetch all the data to be returned from a prepared and executed statement handle. It returns a reference to an array that contains one reference per row.

If there are no rows to return, fetchall_arrayref returns a reference to an empty array. If an error occurs, fetchall_arrayref returns the data fetched thus far, which may be none. You should check $sth->err afterwards (or use the RaiseError attribute) to discover if the data is complete or was truncated due to an error.

When passed an array reference, fetchall_arrayref uses fetchrow_arrayref to fetch each row as an array ref. If the parameter array is not empty then it is used as a slice to select individual columns by index number.

With no parameters, fetchall_arrayref acts as if passed an empty array ref.

When passed a hash reference, fetchall_arrayref uses fetchrow_hashref to fetch each row as a hash reference. If the parameter hash is not empty, then it is used as a slice to select individual columns by name. The names should be lower case regardless of the letter case in $sth->{NAME}. The values of the hash should be set to 1.

For example, to fetch just the first column of every row:

  $tbl_ary_ref = $sth->fetchall_arrayref([0]);

To fetch the second to last and last column of every row:

  $tbl_ary_ref = $sth->fetchall_arrayref([-2,-1]);

To fetch all fields of every row as a hash ref:

  $tbl_ary_ref = $sth->fetchall_arrayref({});

To fetch only the fields called ``foo'' and ``bar'' of every row as a hash ref:

  $tbl_ary_ref = $sth->fetchall_arrayref({ foo=>1, bar=>1 });

The first two examples return a reference to an array of array refs. The last returns a reference to an array of hash refs.

finish
  $rc  = $sth->finish;

Indicates that no more data will be fetched from this statement handle before it is either executed again or destroyed. The finish method is rarely needed, but can sometimes be helpful in very specific situations to allow the server to free up resources (such as sort buffers).

When all the data has been fetched from a SELECT statement, the driver should automatically call finish for you. So you should not normally need to call it explicitly.

Consider a query like:

  SELECT foo FROM table WHERE bar=? ORDER BY foo

where you want to select just the first (smallest) ``foo'' value from a very large table. When executed, the database server will have to use temporary buffer space to store the sorted rows. If, after executing the handle and selecting one row, the handle won't be re-executed for some time and won't be destroyed, the finish method can be used to tell the server that the buffer space can be freed.

Calling finish resets the Active attribute for the statement. It may also make some statement handle attributes (such as NAME and TYPE) unavailable if they have not already been accessed (and thus cached).

The finish method does not affect the transaction status of the database connection. It has nothing to do with transactions. It's mostly an internal ``housekeeping'' method that is rarely needed. There's no need to call finish if you're about to destroy or re-execute the statement handle. See also disconnect and the Active attribute.

rows
  $rv = $sth->rows;

Returns the number of rows affected by the last row affecting command, or -1 if the number of rows is not known or not available.

Generally, you can only rely on a row count after a non-SELECT execute (for some specific operations like UPDATE and DELETE), or after fetching all the rows of a SELECT statement.

For SELECT statements, it is generally not possible to know how many rows will be returned except by fetching them all. Some drivers will return the number of rows the application has fetched so far, but others may return -1 until all rows have been fetched. So use of the rows method or $DBI::rows with SELECT statements is not recommended.

One alternative method to get a row count for a SELECT is to execute a ``SELECT COUNT(*) FROM ...'' SQL statement with the same ``...'' as your query and then fetch the row count from that.

bind_col
  $rc = $sth->bind_col($column_number, \$var_to_bind);

Binds an output column (field) of a SELECT statement to a Perl variable. See bind_columns below for an example. Note that column numbers count up from 1.

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. The binding is performed at a very low level using Perl aliasing so there is no extra copying taking place. This makes using bound variables very efficient.

For maximum portability between drivers, bind_col should be called after execute. This restriction may be removed in a later version of the DBI.

You do not need to bind output columns in order to fetch data, but it can be useful for some applications which need either maximum performance or greater clarity of code. The bind_param method performs a similar but opposite function for input variables.

bind_columns
  $rc = $sth->bind_columns(@list_of_refs_to_vars_to_bind);

Calls bind_col for each column of the SELECT statement. The bind_columns method will die if the number of references does not match the number of fields.

For maximum portability between drivers, bind_columns should be called after execute.

For example:

  $dbh->{RaiseError} = 1; # do this, or check every call for errors
  $sth = $dbh->prepare(q{ SELECT region, sales FROM sales_by_region });
  $sth->execute;
  my ($region, $sales);
  # Bind Perl variables to columns:
  $rv = $sth->bind_columns(\$region, \$sales);
  # you can also use Perl's \(...) syntax (see perlref docs):
  #     $sth->bind_columns(\($region, $sales));
  # Column binding is the most efficient way to fetch data
  while ($sth->fetch) {
      print "$region: $sales\n";
  }

For compatibility with old scripts, the first parameter will be ignored if it is undef or a hash reference.

dump_results
  $rows = $sth->dump_results($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 method is designed as a handy utility for prototyping and testing queries. Since it uses neat_list to format and edit the string for reading by humans, it is not recomended for data transfer applications.

Statement Handle Attributes

This section describes attributes specific to statement handles. Most of these attributes are read-only.

Changes to these statement handle attributes do not affect any other existing or future statement handles.

Attempting to set or get the value of an unknown attribute is fatal, except for private driver specific attributes (which all have names starting with a lowercase letter).

Example:

  ... = $h->{NUM_OF_FIELDS};    # get/read

Note that some drivers cannot provide valid values for some or all of these attributes until after $sth->execute has been called.

See also finish to learn more about the effect it may have on some attributes.

NUM_OF_FIELDS (integer, read-only)
Number of fields (columns) the prepared statement will return. Non-SELECT statements will have NUM_OF_FIELDS == 0.

NUM_OF_PARAMS (integer, read-only)
The number of parameters (placeholders) in the prepared statement. See SUBSTITUTION VARIABLES below for more details.

NAME (array-ref, read-only)
Returns a reference to an array of field names for each column. The names may contain spaces but should not be truncated or have any trailing space. Note that the names have the letter case (upper, lower or mixed) as returned by the driver being used. Portable applications should use NAME_lc or NAME_uc.
  print "First column name: $sth->{NAME}->[0]\n";

NAME_lc (array-ref, read-only)
Like NAME but always returns lowercase names.

NAME_uc (array-ref, read-only)
Like NAME but always returns uppercase names.

TYPE (array-ref, read-only) NEW
Returns a reference to an array of integer values for each column. The value indicates the data type of the corresponding column.

The values correspond to the international standards (ANSI X3.135 and ISO/IEC 9075) which, in general terms, means ODBC. Driver-specific types that don't exactly match standard types should generally return the same values as an ODBC driver supplied by the makers of the database. That might include private type numbers in ranges the vendor has officially registered. See:

  ftp://jerry.ece.umassd.edu/isowg3/dbl/SQL_Registry

Where there's no vendor-supplied ODBC driver to be compatible with, the DBI driver can use type numbers in the range that is now officially reserved for use by the DBI: -9999 to -9000.

All possible values for TYPE should have at least one entry in the output of the type_info_all method (see type_info_all).

PRECISION (array-ref, read-only) NEW
Returns a reference to an array of integer values for each column. For non-numeric columns, the value generally refers to either the maximum length or the defined length of the column. For numeric columns, the value refers to the maximum number of significant digits used by the data type (without considering a sign character or decimal point). Note that for floating point types (REAL, FLOAT, DOUBLE), the ``display size'' can be up to 7 characters greater than the precision. (for the sign + decimal point + the letter E + a sign + 2 or 3 digits).

SCALE (array-ref, read-only) NEW
Returns a reference to an array of integer values for each column. NULL (undef) values indicate columns where scale is not applicable.

NULLABLE (array-ref, read-only)
Returns a reference to an array indicating the possibility of each column returning a null. Possible values are 0 = no, 1 = yes, 2 = unknown.
  print "First column may return NULL\n" if $sth->{NULLABLE}->[0];

CursorName (string, read-only)
Returns the name of the cursor associated with the statement handle, if available. If not available or if the database driver does not support the "where current of ..." SQL syntax, then it returns undef.

Statement (string, read-only) NEW
Returns the statement string passed to the prepare method.

RowsInCache (integer, read-only)
If the driver supports a local row cache for SELECT statements, then this attribute holds the number of un-fetched rows in the cache. If the driver doesn't, then it returns undef. Note that some drivers pre-fetch rows on execute, whereas others wait till the first fetch.

See also the RowCacheSize database handle attribute.


FURTHER INFORMATION

Transactions

Transactions are a fundamental part of any robust database system. They protect against errors and database corruption by ensuring that sets of related changes to the database take place in atomic (indivisible, all-or-nothing) units.

This section applies to databases that support transactions and where AutoCommit is off. See AutoCommit for details of using AutoCommit with various types of databases.

The recommended way to implement robust transactions in Perl applications is to use RaiseError and eval { ... } (which is very fast, unlike eval "..."). For example:

  $dbh->{AutoCommit} = 0;  # enable transactions, if possible
  $dbh->{RaiseError} = 1;
  eval {
      foo(...)        # do lots of work here
      bar(...)        # including inserts
      baz(...)        # and updates
      $dbh->commit;   # commit the changes if we get this far
  };
  if ($@) {
      warn "Transaction aborted because $@";
      $dbh->rollback; # undo the incomplete changes
      # add other application on-error-clean-up code here
  }

If the RaiseError attribute is not set, then DBI calls would need to be manually checked for errors, typically like this:

  $h->method(@args) || die $h->errstr;

With RaiseError set, the DBI will automatically die if any DBI method call on that handle (or a child handle) fails, so you don't have to test the return value of each method call. See RaiseError for more details.

A major advantage of the eval approach is that the transaction will be properly rolled back if any code (not just DBI calls) in the inner application dies for any reason. The major advantage of using the $h->{RaiseError} attribute is that all DBI calls will be checked automatically. Both techniques are strongly recommended.

Handling BLOB / LONG / Memo Fields

Many databases support ``blob'' (binary large objects), ``long'', or similar datatypes for holding very long strings or large amounts of binary data in a single field. Some databases support variable length long values over 2,000,000,000 bytes in length.

Since values of that size can't usually be held in memory, and because databases can't usually know in advance the length of the longest long that will be returned from a SELECT statement (unlike other data types), some special handling is required.

In this situation, the value of the $h->{LongReadLen} attribute is used to determine how much buffer space to allocate when fetching such fields. The $h->{LongTruncOk} attribute is used to determine how to behave if a fetched value can't fit into the buffer.

When trying to insert long or binary values, placeholders should be used since there are often limits on the maximum size of an INSERT statement and the quote method generally can't cope with binary data. See Placeholders and Bind Values.

Simple Examples

Here's a complete example program to select and fetch some data:

  my $dbh = DBI->connect("dbi:DriverName:db_name", $user, $password)
      || die "Can't connect to $data_source: $DBI::errstr";
  my $sth = $dbh->prepare( q{
          SELECT name, phone
          FROM mytelbook
  }) || die "Can't prepare statement: $DBI::errstr";
  my $rc = $sth->execute
      || die "Can't execute statement: $DBI::errstr";
  print "Query will return $sth->{NUM_OF_FIELDS} fields.\n\n";
  print "Field names: @{ $sth->{NAME} }\n";
  while (($name, $phone) = $sth->fetchrow_array) {
      print "$name: $phone\n";
  }
  # check for problems which may have terminated the fetch early
  die $sth->errstr if $sth->err;
  $dbh->disconnect;

Here's a complete example program to insert some data from a file. (This example uses RaiseError to avoid needing to check each call).

  my $dbh = DBI->connect("dbi:DriverName:db_name", $user, $password, {
      RaiseError => 1, AutoCommit => 0
  });
  my $sth = $dbh->prepare( q{
      INSERT INTO table (name, phone) VALUES (?, ?)
  });
  open FH, "<phone.csv" or die "Unable to open phone.csv: $!";
  while (<FH>) {
      chop;
      my ($name, $phone) = split /,/;
      $sth->execute($name, $phone);
  }
  close FH;
  $dbh->commit;
  $dbh->disconnect;

Here's how to convert fetched NULLs (undefined values) into empty strings:

  while($row = $sth->fetchrow_arrayref) {
    # this is a fast and simple way to deal with nulls:
    foreach (@$row) { $_ = '' unless defined }
    print "@$row\n";
  }

The q{...} style quoting used in these examples avoids clashing with quotes that may be used in the SQL statement. Use the double-quote like qq{...} operator if you want to interpolate variables into the string. See perlop/``Quote and Quote-like Operators'' for more details.

Threads and Thread Safety

Perl versions 5.004_50 and later include optional experimental support for multiple threads on many platforms. If the DBI is built using a Perl that has threads enabled then it will use a per-driver mutex to ensure that only one thread is with a driver at any one time. Please note that support for threads in Perl is still experimental and is known to have some significant problems. It's use is not recommended.

Signal Handling and Canceling Operations

The first thing to say is that signal handling in Perl is currently not safe. There is always a small risk of Perl crashing and/or core dumping when, or after, handling a signal. (The risk was reduced with 5.004_04 but is still present.)

The two most common uses of signals in relation to the DBI are for canceling operations when the user types Ctrl-C (interrupt), and for implementing a timeout using alarm() and $SIG{ALRM}.

To assist in implementing these operations, the DBI provides a cancel method for statement handles. The cancel method should abort the current operation and is designed to be called from a signal handler.

However, it must be stressed that: a) few drivers implement this at the moment (the DBI provides a default method that just returns undef); and b) even if implemented, there is still a possibility that the statement handle, and possibly the parent database handle, will not be usable afterwards.

If cancel returns true, then it has successfully invoked the database engine's own cancel function. If it returns false, then cancel failed. If it returns undef, then the database engine does not have cancel implemented.


DEBUGGING

In addition to the trace method, you can enable the same trace information by setting the DBI_TRACE environment variable before starting Perl.

On Unix-like systems using a Bourne-like shell, you can do this easily on the command line:

  DBI_TRACE=2 perl your_test_script.pl

If DBI_TRACE is set to a non-numeric value, then it is assumed to be a file name and the trace level will be set to 2 with all trace output appended to that file. If the name begins with a number followed by an equal sign (=), then the number and the equal sign are stripped off from the name, and the number is used to set the trace level. For example:

  DBI_TRACE=1=dbitrace.log perl your_test_script.pl

See also the trace method.


WARNING AND ERROR MESSAGES

Fatal Errors

Can't call method ``prepare'' without a package or object reference
The $dbh handle you're using to call prepare is probably undefined because the preceding connect failed. You should always check the return status of DBI methods, or use the RaiseError attribute.

Can't call method ``execute'' without a package or object reference
The $sth handle you're using to call execute is probably undefined because the preceeding prepare failed. You should always check the return status of DBI methods, or use the RaiseError attribute.

DBI/DBD internal version mismatch
The DBD driver module was built with a different version of DBI than the one currently being used. You should rebuild the DBD module under the current version of DBI.

(Some rare platforms require ``static linking''. On those platforms, there may be an old DBI or DBD driver version actually embedded in the Perl executable being used.)

DBD driver has not implemented the AutoCommit attribute
The DBD driver implementation is incomplete. Consult the author.

Can't [sg]et %s->{%s}: unrecognised attribute
You attempted to set or get an unknown attribute of a handle. Make sure you have spelled the attribute name correctly; case is significant (e.g., ``Autocommit'' is not the same as ``AutoCommit'').

Warnings

Database handle destroyed without explicit disconnect
A $dbh handle went out of scope or the program ended before the handle was disconnected from the database. You should always explicitly call disconnect when you are finished using a database handle. If using transactions then you should also explicitly call commit or rollback before disconnect.

DBI Handle cleared whilst still holding %d cached kids!
Most probably due to a DBI bug. Possibly a DBD driver bug. Please report it.

DBI Handle cleared whilst still active!
Most probably due to a DBI bug. Possibly a DBD driver bug. Please report it.

DBI Handle has uncleared implementors data
Most probably a DBD driver bug. Please report it.

DBI Handle has %d uncleared child handles
Most probably due to a DBI bug. Possibly a DBD driver bug. Please report it.


SEE ALSO

Driver and Database Documentation

Refer to the documentation for the DBD driver that you are using.

Refer to the SQL Language Reference Manual for the database engine that you are using.

Books and Journals

 Programming the Perl DBI, by Alligator Descartes and Tim Bunce.
 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 users of the DBI and its related modules. Subscribe and unsubscribe via:

 http://www.isc.org/dbi-lists.html

There are typically between 700 and 900 messages per month. You have to subscribe in order to be able to post. However you can opt for a 'post-only' subscription.

Mailing list archives are held at:

 http://www.xray.mpe.mpg.de/mailing-lists/dbi/
 http://www.egroups.com/list/dbi-users/info.html
 http://www.bitmechanic.com/mail-archives/dbi-users/

Assorted Related WWW Links

The DBI ``Home Page'':

 http://www.symbolstone.org/technology/perl/DBI

Other DBI related links:

 http://tegan.deltanet.com/~phlip/DBUIdoc.html
 http://dc.pm.org/perl_db.html
 http://wdvl.com/Authoring/DB/Intro/toc.html
 http://www.hotwired.com/webmonkey/backend/tutorials/tutorial1.html
 http://bumppo.net/lists/macperl/1999/06/msg00197.html

Other database related links:

 http://www.jcc.com/sql_stnd.html
 http://cuiwww.unige.ch/OSG/info/FreeDB/FreeDB.home.html

Commercial and Data Warehouse Links

 http://www.dwinfocenter.org
 http://www.datawarehouse.com
 http://www.datamining.org
 http://www.olapcouncil.org
 http://www.idwa.org
 http://www.knowledgecenters.org/dwcenter.asp

Recommended Perl Programming Links

 http://language.perl.com/style/

FAQ

Please also read the DBI FAQ which is installed as a DBI::FAQ module. You can use perldoc to read it by executing the perldoc DBI::FAQ command.


AUTHORS

DBI by Tim Bunce. This pod text by Tim Bunce, J. Douglas Dunlop, Jonathan Leffler and others. Perl by Larry Wall and the perl5-porters.


COPYRIGHT

The DBI module is Copyright (c) 1994-2000 Tim Bunce. England. All rights reserved.

You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.


ACKNOWLEDGEMENTS

I 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). In no particular order: Kevin Stock, Buzz Moschetti, Kurt Andersen, Ted Lemon, William Hails, Garth Kennedy, Michael Peppler, Neil S. Briscoe, Jeff Urlwin, 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.

Then, of course, there are the poor souls who have struggled through untold and undocumented obstacles to actually implement DBI drivers. Among their ranks are Jochen Wiedmann, Alligator Descartes, Jonathan Leffler, Jeff Urlwin, Michael Peppler, Henrik Tougaard, Edwin Pratomo, Davide Migliavacca, Jan Pazdziora, Peter Haworth, Edmund Mergl, Steve Williams, Thomas Lowery, and Phlip Plumlee. Without them, the DBI would not be the practical reality it is today. I'm also especially grateful to Alligator Descartes for starting work on the ``Programming the Perl DBI'' book and letting me jump on board.


TRANSLATIONS

A German translation of this manual and other Perl module docs (all probably slightly out of date) is available, thanks to O'Reilly, at:

  http://www.oreilly.de/catalog/perlmodger/

Some other translations:

 http://cronopio.net/perl/                              - Spanish
 http://member.nifty.ne.jp/hippo2000/dbimemo.htm        - Japanese


SUPPORT / WARRANTY

The DBI is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.

Commercial support for Perl and the DBI, DBD::Oracle and Oraperl modules can be arranged via The Perl Clinic. For more details visit:

  http://www.perlclinic.com


TRAINING

References to DBI related training resources. No recommendation implied.

  http://www.treepax.co.uk/
  http://www.keller.com/dbweb/


FREQUENTLY ASKED QUESTIONS

See the DBI FAQ for a more comprehensive list of FAQs. Use the perldoc DBI::FAQ command to read it.

How fast is the DBI?

To measure the speed of the DBI and DBD::Oracle code, I modified DBD::Oracle so you can set an attribute that will cause the same row to be fetched from the row cache over and over again (without involving Oracle code but exercising *all* the DBI and DBD::Oracle code in the code path for a fetch).

The results (on my lightly loaded old Sparc 10) fetching 50000 rows using:

        1 while $csr->fetch;

were:
one field: 5300 fetches per cpu second (approx)
ten fields: 4000 fetches per cpu second (approx)

Obviously results will vary between platforms (newer faster platforms can reach around 50000 fetches per second), but it does give a feel for the maximum performance: fast. By way of comparison, using the code:

        1 while @row = $csr->fetchrow_array;

(fetchrow_array is roughly the same as ora_fetch) gives:

        one field:   3100 fetches per cpu second (approx)
        ten fields:  1000 fetches per cpu second (approx)

Notice the slowdown and the more dramatic impact of extra fields. (The fields were all one char long. The impact would be even bigger for longer strings.)

Changing that slightly to represent actually doing something in Perl with the fetched data:

    while(@row = $csr->fetchrow_array) {
        $hash{++$i} = [ @row ];
    }

gives: ten fields: 500 fetches per cpu second (approx)

That simple addition has *halved* the performance.

I therefore conclude that DBI and DBD::Oracle overheads are small compared with Perl language overheads (and probably database overheads).

So, if you think the DBI or your driver is slow, try replacing your fetch loop with just:

        1 while $csr->fetch;

and time that. If that helps then point the finger at your own code. If that doesn't help much then point the finger at the database, the platform, the network etc. But think carefully before pointing it at the DBI or your driver.

(Having said all that, if anyone can show me how to make the DBI or drivers even more efficient, I'm all ears.)

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/cgi-bin/pace/pub/doc/FAQs/cgi/perl-cgi-faq.html
 http://www3.pair.com/webthing/docs/cgi/faqs/cgifaq.shtml
 http://www-genome.wi.mit.edu/WWW/faqs/www-security-faq.html
 http://www.boutell.com/faq/
 http://www.perl.com/perl/faq/

General problems and good ideas:

 Use the CGI::ErrorWrap module.
 Remember that many env vars won't be set for CGI scripts.

How can I maintain a WWW connection to a database?

For information on the Apache httpd server and the mod_perl module see

  http://perl.apache.org/

What about ODBC?

A DBD::ODBC module is available.

Does the DBI have a year 2000 problem?

No. The DBI has no knowledge or understanding of dates at all.

Individual drivers (DBD::*) may have some date handling code but are unlikely to have year 2000 related problems within their code. However, your application code which uses the DBI and DBD drivers may have year 2000 related problems if it has not been designed and written well.

See also the ``Does Perl have a year 2000 problem?'' section of the Perl FAQ:

  http://www.perl.com/CPAN/doc/FAQs/FAQ/PerlFAQ.html


OTHER RELATED WORK AND PERL 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.

JDBC Server by Stuart 'Zen' Bishop zen@bf.rmit.edu.au
The server is written in Perl. The client classes that talk to it are of course in Java. Thus, a Java applet or application will be able to comunicate via the JDBC API with any database that has a DBI driver installed. The URL used is in the form jdbc:dbi://host.domain.etc:999/Driver/DBName. It seems to be very similar to some commercial products, such as jdbcKona.

Remote Proxy DBD support
As of DBI 1.02, a complete implementation of a DBD::Proxy driver and the DBI::ProxyServer are part of the DBI distribution.

SQL Parser
        Hugo van der Sanden <hv@crypt.compulink.co.uk>
        Stephen Zander <stephen.zander@mckesson.com>

Based on the O'Reilly lex/yacc book examples and byacc.

See also the SQL::Statement module, a very simple SQL parser and engine, base of the DBD::CSV driver.