Perl Module Mania: DBI and DBD

Given our focus on a Windows-based database in this particular article, we will also work with the Windows version of Perl -- the most popular of which is the ActiveState port, also known as ActivePerl. In almost all respects that interest us, using ActivePerl is much the same as Perl 5 in a Unix environment.

One of the nice perks of ActivePerl is that it is easy to install additional Perl modules. ActivePerl's PPM, or Perl Package Manager, is a command-line interface with which you can simply request modules from ActivePerl's Internet site and they are automatically downloaded and installed. That's good because we need some Perl modules! We'll detail the process for Unix users next month, but if you are a Unix user with experience installing Perl modules already, this month's coverage will probably suffice.

No matter what database you intend to work with from Perl, be it Access or Oracle or MySQL and so on, you will want to install and use the Perl module DBI, or Database Independent Interface (but it's not called DBII!). The DBI module provides a simple and consist means of querying a database regardless of the source format of that database. This allows us Perl coders to query Oracle databases with the same Perl script that we query Access databases, for instance.

We'll look more closely at using DBI in Perl code soon -- but first you need to retrieve and install the DBI module. Assuming you have installed ActivePerl, launch an MS-DOS prompt in Windows and type "ppm" to launch the Perl Package Manager. From there you simply enter the command "install DBI" and PPM should take care of the rest.

PPM interactive shell (1.0.0) - type 'help' for available commands.
	PPM>install DBI
	Install package 'DBI?' (y/N): y
	Retrieving package 'DBI'...

Just how smart is this DBI module? How can it possibly know how to talk to Oracle databases, Informix databases, MS Access, and so on? In fact, DBI doesn't know any of this. DBI only knows how to talk to DBD. What??

DBD modules contain the "brains" for DBI ... thus there is a DBI module specifically written for each brand of database that you can query with DBI. For example, any database that can communicate via the ODBC protocol can be queried with DBI and the DBD::ODBC module. Alternatively, you could query an Oracle database with DBI and DBD::Oracle, a Sybase database with DBI and DBD::Sybase, and so on. If you're using a database we haven't mentioned here, check the current list of DBI Module Availability.

Since MS Access supports ODBC, we want to install the DBD::ODBC module. To do so, return to the Perl Package Manager:

PPM interactive shell (1.0.0) - type 'help' for available commands.
	PPM>install DBD-ODBC
	Install package 'DBD-ODBC?' (y/N): y
	Retrieving package 'DBD-ODBC'...

That's all ... finally, we are ready to code some Perl!