DBI Class Methods

The DBI class methods are those supplied from the class as a whole. The most important is the connect() method, which, upon success, will return a database handle.

available_drivers

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

Returns a list of available drivers (DBD modules). This gives a warning if there are drivers with the same name. Setting the optional $quiet to true stills the warning.

connect

$dbh = DBI->connect($datasource, $username, $password [, \%attributes]);

Creates a connection to the database using the specified data source, username, and password and returns a database handle. The data source consists of the DBI driver (in this case, dbi:mysql), the database name, an optional hostname (localhost if unspecified), an optional port name (3306 if unspecified) and a number of modifiers, each separated by a semicolon.

mysql_read_default_file=filename

The specified file is used as an option file (MySQL configuration file, usually my.ini or my.cnf on the server).

mysql_read_default_group=groupname

When reading an option file, the default group to use is [client]. This changes the group to [groupname].

The following option causes communication between the client and server to be compressed:

mysql_compression=1

The following option specifies the path to the Unix socket used to connect to the server:

mysql_socket=/path/to/socket

The optional username and password will take the values of the DBI_USER and DBI_PASS environment variables if not specified.

If the connection fails, it will return undef and sets $DBI:err and $DBI:errstr.

You can use the \%attributes parameter to set the various attributes, such as AutoCommit (recommended), RaiseError, and PrintError.

For example:

my $hostname = 'localhost'; my $database = 'firstdb'; my $username = 'guru2b'; my $password = 'g00r002b'; #Connect to the database  my $dbh = DBI->connect("dbi:mysql:$database:$hostname", $username,–  $password, (AutoCommit => 0, RaiseError => 1, PrintError => 0)) or die $DBI::errstr; 

connect_cached

$dbh = DBI->connect_cached($data_source, $username, $password[ , \%attributes])

Just like connect(), except that the details of the database handle are also stored in a hash array. This same database handle is used for further identical calls to connect_cached() if it's still valid. The CachedKids attribute (accessed via $dbh->{Driver}->{CachedKids}) contains cache data.

This is still a fairly new method and is likely to change. It is not the same as an Apache::DBI persistent connection.

data_sources

@ary = DBI->data_sources($driver [, \%attributes]);

Returns an array of all databases available to the named driver (mysql in this case).

trace

trace($trace_level [, $trace_filename])

This method enables or disables tracing. When called as a DBI class method, it affects tracing for all database and statement handles. When called as a database or statement handle method, it affects tracing for that handle (and future children of that handle).

The trace level can be from 0 to 9, with 0 disabling tracing, 1 best for a general overview,
2 being the most commonly used, and the other methods adding more and more driver and DBI detail.

Output goes to STDERR by default or is appended to the trace file if one was specified.

For example:

DBI->trace(2);                         # trace everything $dth->trace(2, "/tmp/dbi_trace.out");  # trace the database handle                                        # to /tmp/dbi_trace.out $sth->trace(2);                        # trace the statement handle

You can also enable tracing by setting the DBI_TRACE environment variable to either a num-
ber (0–9) or a file, in which case tracing will be set at level 2 and output to that file.



Mastering MySQL 4
Mastering MySQL 4
ISBN: 0782141625
EAN: 2147483647
Year: 2003
Pages: 230
Authors: Ian Gilfillan

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net