Appendix D: Perl DBI

Overview

The recommended way to connect to a database (not only to MySQL) in Perl is with the DBI module. This is a generic interface that allows you to access different kinds of databases in the same way. Along with the DBI module, you need a DBD module. Each DBD module is for a specific database; you should use the one associated with MySQL.

Note 

To install Perl DBI support for MySQL, you need the DBI, DBD-mysql, Data-Dumper, and File-Spec modules. You can download the latest versions from www.mysql.com/CPAN. Full installation instructions come with the software.

Throughout this appendix, you'll see the following conventions for variable names:

$dbh  A database handle object, returned from the connect() or connect_cached() methods

$sth  A statement handle object, returned from a prepare() method among others

$drh  A driver handle object (rarely used in applications)

$h  A database, statement, or driver handle

$rc  A Boolean return code (true for success or false for an error)

$rv  A return value of sorts, usually an integer

@ary  An array of values, usually a row of data returned from a query

$rowsThe number of rows processed, or -1 if unknown

$fh  A file handle

undef  A NULL, or undefined, value

\%attributes  A reference to a hash of attribute values, used for various purposes by the methods

To use the DBI, you need to load the DBI module at the beginning of your script, as follows:

use DBI;

Then, you need to return a database handle, usually with the connect() DBI class method. The database handle then accesses methods that can run queries and return results, usually returning a statement handle.



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