DBI Perl Module
DBI Perl Module (DBI stands for database-independent interface) is one of the most important Perl module designed to allow the use of SQl and mySQL queries, available on most platforms, including SQL Server, DB2, Oracle and MySQL.
This module allows the connection from a Perl script to different databases, using the appropriate database driver (DBD).
The DBI Perl module (which is database independent) is responsible for loading at runtime the appropriate DBI driver which interfaces with the specific database.
For example, if we want to connect to MySQL database, we must use DBI and DBD::MySQL modules in the Perl script. We can use the driver DBD::ODBC too, which allow us to connect to many databases such as MS Access, Paradox, Oracle and so on. In the next figure you can see the architecture of a DBI Application.
There is a simple interface to specify what kind of SQL queries you want to do to get the results you want. When you launch a SQL query through the DBI Perl module, the request will be sent to the DBD module corresponding to the database you want to use, which is responsible for the correct implementation of the demand.
Thus it is not necessary to know operational details of implementing the various databases, DBD modules providing the necessary interface and transmitting back to the DBI module results from the execution of the request.
DBD modules convert queries received from DBI module in a format according to the requested libraries database and return results to the DBI Perl module as a Perl data structure. In this way, the same script could use more DBD modules for access to various databases.
To use the DBI Perl module in a Perl script, you need to have previously installed:
- Software database that you want to use (eg. MySQL)
- DBI module
- DBD module (for example if we want to use MySQL, it is necessary to install DBD::mysql module)
We will exemplify by presenting a few considerations on the installation and use of DBI and DBD modules for ActivePerl under Windows.
Download and install DBI
In order to install the Perl DBI module, we’ll simply use the Perl Package Manager (PPM) application which is included in ActivePerl.
On my computer, ActivePerl is installed in the directory: C:\Perl and it has the 5.8.8 version. If you want to see what ActivePerl version you have installed on your computer, you can run:
To download the appropriate version of DBI for ActivePerl, go to
and choose the appropriate zip archive – let’s see it is DBI-1.602.zip (8xx is for Perl 5.8 version).
I unzipped the archive in the directory C:\Perl\DBI-1.602, I opened a command prompt window and I changed the directory where there is the DBI installation file (in order to install DBI, we must run the command "ppm install DBI.ppd" in the current directory where there is the DBI.ppd installation file):
C:\Documents and Settings\Administrator>cd C:\Perl\DBI-1.602
C:\Perl\DBI-1.602>ppm install DBI.ppd
Generating HTML for DBI-602…done
Installing to site area…done
115 files installed
2 files updated
If you have DBI installed and if you want to see if you have a recent enough version of DBI (after 1.40 is OK), you can run a command like that:
and you’ll get the information:
DBI version 2 required—this is only version 1.602 …
Of course, 2.00 version doesn’t exist yet, but you can use such a trick to find out your DBI installed version.
If your DBI module is not installed, you’ll get an error saying DBI cannot be found in @INC.
The next step is to install the DBD driver in order to connect to a specific database. We’ll go on with an example by connecting to a Paradox database through DBD::ODBC module. Of course, through ODBC you can connect to many kinds of database including Oracle and MS Access, but I chose Paradox in order to exemplify.
If you want to see how you can install and use such a DBD driver, please follow the DBD appropriate link below.
Return from DBI Perl Module to Perl Database Programming