Installing DBI and Using MySQL with Perl in Windows

You will learn in this tutorial how to install the DBI (the Perl interface to databases) in Windows (NT, XP, 2000). There is another version for Linux here. You can obtain a more detailed introduction to DBI by reading its FAQ. You can see below the basic DBI model. The scripts are written in Perl using standard Perl variables, commands and syntax. The DBI has methods and handles which are database software independent. You program to access, change or query a database using the standard SQL language, combined with the DBI methods and handles. You install as many DBD::type modules as you need to support the different database software you may have, but you Perl script will be the same.

You will also see in this tutorial the basics of using DBI to perform queries, and display the results. Once you learn the basics you can use Perl to program the other SQL commands we saw briefly in our class on MySQL, and you should have learned in more details in the DB course. Please note that we have on reserve at the Library the book Programming the Perl DBI, the authoritative reference to DBI.


You will need to download and install three pieces of software: DB_File, DBI, Table and MySQL DBD module. If you were installing support for another database like Oracle, Informix, etc, you would also have to download their DBD modules.

The installation in Windows relies in CPAN (Comprehensive Perl Archive Network) archives and PPM (Programmer's Package Manager) from ActiveState.

  1. When you installed Perl, PPM was installed automatically and it allows you to install and manages modules, originally available in CPAN.
  2. you open a Command Prompt Window and type ppm. An interactive session starts (requires you to be connected to the Internet to search the modules repository) and you can search for the module you want to install, and later install it.
  3. finally at the ppm prompt type install package-name (package-name is the name you want to install after you found it) and PPM will install the module in the Perl/lib directory.

  1. Install DBI
  2. Install DB_File first. At the ppm prompt type search DB_File and if it is found (it will), then type install DB_FIle (see it done). Proceed to download DBI, the Perl module to interface with Ddatabases. You will only need to download one version of DBI to use with any database: MySQL, Oracle, Informix, etc.

    Once more at the PPM prompt type search DBI and you will see a variety of modules you can install, related to DBI. Install the main DBI module as shown here. The following script let you test if your installation is working fine:

    #!/usr/bin/perl -w
    use strict;
    use DB_File;
    my %database;
    tie %database, 'DB_File', "createdb.dat" or die "Can't initialize database: $!\n";
    untie %database;
    If all went well a file createdb.dat will be created by the script.

  3. Install Data-Table
  4. You will only need to install one version of Table to use with any database: MySQL, Oracle, Informix, etc. Again, at the ppm prompt type search Data-Table and when found type install Data-Table, as shown here.

  5. Install the MySQL module
  6. You will need to download one version of the DBD module to use with each database: MySQL, Oracle, Informix, etc. These instructions refer to downloading and installing the DBD module for MySQL, but you can install more than one of the modules to access different types of database software. The same Perl commands wich work for one database type will work for the other types: the code is portable not only across platforms, but also across database software.

    Once more, at the ppm prompt type search DBD and see the variety of modules available. You are only interested in installing the driver for Mysql and at the ppm prompt type install DBD-Mysql and this is it.

      This completes the installation of Perl modules to manipulate MySQL databases. The following script let you test if your installation is working fine with MySQL, assuming you have created a MYTEST database and the root password is rootpass:

      #!/usr/bin/perl  -w
      use DBI;
      #definition of variables
      $password="rootpass";  # the root password
      #connect to MySQL database
      my $dbh   = DBI->connect ("DBI:mysql:database=$db:host=$host",
                                 or die "Can't connect to database: $DBI::errstr\n";
      #disconnect  from database 
      $dbh->disconnect or warn "Disconnection error: $DBI::errstr\n";

    Using MySQL with the Perl DBI Interface

    The basic documentation for the MySQL module is the reference table and methods for the DBI Interface, including specific commands only available in MySQL.

    Lets see some basic concepts and a series of examples to illustrate them.

    You are not limited to queries when using DBI. You can create and drop tables, insert, update, etc.

    This page is maintained by Al Bento who can be reached at This page was last updated on October 28, 2002. Although we will attempt to keep this information accurate, we can not guarantee the accuracy of the information provided.