Perl Programming/DBI - Perl database interface

=DBI - Perl database interface=

There is a comprehensive library of modules for connecting to databases from Perl programs. It is maintained by Tim Bunce and it is called DBI - database interface. The main Website for DBI is http://dbi.perl.org/.

Installing the necessary modules
To use DBI to connect to a database, you need at least two CPAN modules. One is the main DBI module, simply called DBI. The other one is a DBD - a database driver module. There are DBI drivers for many mainstream database management systems, such as MySQL and Oracle. The examples in this tutorial concern Oracle; accordingly, the database driver for Oracle is called DBD::Oracle.

So, to run the examples in this chapter you need the modules DBI and DBD::Oracle.

Installing DBI
Installing DBI is straightforward. (For information about installing CPAN modules, see the chapter about CPAN.)

Installing DBD::Oracle
When installing DBD::Oracle, there are a few things that you need to prepare.

Getting the correct version
First of all, when installing DBD::Oracle, make sure that you get a version by Pythian. Pythian maintains DBD::Oracle since version 1.17 (February 2006).

Preparing environment variables
You need to create the environment variables ORACLE_USERID and ORACLE_SID. They are needed to test the connection to your database after DBD::Oracle is installed. The value for ORACLE_USERID must be a valid username and password for your database. ORACLE_SID must be set to database name as it appears in TNSNAMES.ORA. On a Unix-like system run:

<tt>export ORACLE_USERID="username/password"

export ORACLE_SID="DATABASE_NAME"</tt>

On Windows, you only need <tt>ORACLE_USERID</tt>:

<tt>set ORACLE_USERID="username/password@DATABASE_NAME"</tt>

When you have finished defining these environment variables, install <tt>DBD::Oracle</tt> from CPAN the usual way.

Using DBI
Here is a very simple test script to get you going.