Skip to main content

How to Connect to MySQL from Perl

How do I connect to a MySQL database from a perl program?use perl DBI module to connect to a MySQL database as explained below.

If you don’t have perl DBI and DBD::mysql module installed, install perl module using cpan as we discussed earlier.
# perl -MCPAN -e shell
cpan> install DBI
cpan> install DBD::mysql
On a very high level, you’ll have to do the following three steps to connect to a MySQL database and get data.

1. Connect to the MySQL Database

In the DBI module, you’ll use the connect function as shown below.
$dbc = DBI->connect($source, $username, $password)
DBI->connect function takes the following three arguments:
  • $source – This is in the format of “DBI:mysql:[database]:[hostname]”. Replace the [database] and [hostname] with values from your system. In the example shown below, it is connecting to the database called “mycompany” that is running on the localhost.
  • $username – The username that is used to connect to the MySQL database.
  • $password – The password for the above username.
You should store the return value of the DBI->connect to a variable, which you’ll use to prepare the sql statement later.

2. Prepare the SQL Statement

Once you’ve connected, you’ll use the prepare the sql statement using the prepare command. You’ll call the prepare command by using the variable that was returned by the DBI->connect function.
The following command will prepare the select sql command.
$sql = $dbc->prepare("select id, name from employee");
You should store the return value of the prepare command to a variable, which you’ll use to execute the sql command.

3. Execute the SQL Statement

Once you’ve prepared the sql statement, you’ll just execute the statement using execute command. You’ll call the execute command by using the variable that was returned by the prepare function.
$out = $sql->execute()

4. Loop through the Records

Once you’ve executed the statement, you’ll loop through records using the fetchrow_array function. You’ll call the fetchrow_array function using the variable that was returned by the prepare function.
while (($id, $name) = $sql->fetchrow_array())
{
 print "Id: $id Name: $name\n";
}

5. Working MySQL Perl Example

The following example perl code will connect a MySQL database and select the records from the employee table and print it.
$ vi connect.pl
#!/usr/bin/perl

use DBI;

$source = "DBI:mysql:mycompany:localhost";
$username = "root";
$password = "MySecretPassword";

$dbc = DBI->connect($source, $username, $password)
or die "Unable to connect to mysql: $DBI::errstr\n";

$sql = $dbc->prepare("select id, name from employee");

$out = $sql->execute()
or die "Unable to execute sql: $sql->errstr";

while (($id, $name) = $sql->fetchrow_array())
{
 print "Id: $id Name: $name\n";
}
When you execute the above connect.pl perl code, it will display the records from the employee table as shown below.
$ ./connect.pl
Id: 100 Name: Thomas
Id: 200 Name: Jason
Id: 300 Name: Sanjay
Id: 400 Name: Nisha
Id: 500 Name: Randy
Id: 501 Name: Ritu

Comments

Popular posts from this blog

20 perl programming tips

1. List all Installed Perl Modules from Unix Command Line Get a list of all installed perl modules as shown below. $ perl -MFile::Find=find -MFile::Spec::Functions -Tlw -e 'find { wanted => sub { print canonpath $_ if /\.pm\z/ }, no_chdir => 1 }, @INC' /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/HTML/Filter.pm /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/HTML/LinkExtor.pm /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/HTML/PullParser.pm /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/HTML/Parser.pm /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/HTML/TokeParser.pm ..... In the above example, File::Find  and  File::Spec::Functions  module are used to list all installed modules. -M option  loads the module. It executes  use module  before executing the script -T option  enables taint checking, which instructs perl to keep track of data from the user and avoid doing anything insecure w...

Perl and Excel: How to Read, Write, Parse Excel Files using Perl

If you want to manipulate excel files programmatically, you can use Perl Spreadsheet module, which provides an object interface that makes it easier to create and parse Excel files. Install Spreadsheet WriteExcel Module Method 1: Standard install using make Download the zipped tar file of  Spreadsheet-ParseExcel  and  Spreadsheet-WriteExcel  from cpan. Untar and unzip the module as follows: tar -zxvf Spreadsheet-WriteExcel.tar.gz cd to the directory the tar creates. Execute the steps below to to install the Spreadsheet-WriteExcel module. perl Makefile.PL make make test make install Use the above procedure to install Spreadsheet-ParseExcel also. Method 2: CPAN.pm install If you have CPAN.pm configured you can install the module as follows: perl -MCPAN -e 'install "Spreadsheet::WriteExcel"' perl -MCPAN -e 'install "Spreadsheet::ParseExcel"' For additional installation help refer to: How To Install Perl Modules Manually and Using ...