For MySQL database connectivity with Perl we have to install following Perl module first.
DBI
DBD::MySQL
DBD::MySQL Perl module which works with the DBI module for Perl Database connectivity.This article gives an example to make MySQL database connectivity with Perl.
DBI stands for database interface.This module creates a bridge between Perl and database.Perl is capable to perform verities of database operations , including INSERT,UPDATE,DELETE, etc through the Perl module DBI. Any functions available with DBI work with all database SQL queries , including SQL Server, DB2,MySQL and Oracle.
Use following command to install DBD::MySQL module.
ppm install http://theoryx5.uwinnipeg.ca/ppms/DBD-mysql.ppd
There are different way to make MySQL database connection with Perl.
I have mentioned two different methods for MySQL database connection with Perl.
Method-I
This is a default method ,this Perl database connection method connects to local database to default database port number.This Perl database connection method need three arguments, like database name,database username,and password.
Following is an example of Perl sub routine for MySQL database connection.This subroutine made database connection and return database connection handle to calling program.
sub db_connect
{
my ($db_name,$db_username,$db_password)=@_;
my $H_db = DBI->connect(“DBI:mysql:$db_name”,$db_username,$db_password, {
RaiseError => 1, AutoCommit => 0, PrintError => 0})
|| die “Unable to perform Database connect to $db_name: $DBI::errstrn”;
return $H_db;
}
Method -II
This type of MySQL database connection,connects local as well as remote database server. We can connect any database host and any non default port number.This type of Perl database connection needs two new parameters and compared to method-I like hostname and port number of the database database.
Following is an example of Perl sub routine for MySQL database connection.This subroutine made database connection and return database connection handle to calling program.
sub db_Connect
{
my ($db_name,$db_user,$db_password,$host,$port)=@_;
my $H_db = DBI->connect(“DBI:MySQL:$db_name:$host:$port”,$db_user,$db_password, {
RaiseError => 1, AutoCommit => 0, PrintError => 0})
|| die “Unable to perform Database connect to $db_name: $DBI::errstrn”;
return $H_db;
}
Database disconnection
Following subroutine check database connection exists or not. If exists then close the connection.
sub db_disconnect {
my $dbh=shift(@_);
if ($dbh) {
$dbh->disconnect() || die “Unable to perform Database disconnect: $DBI::errstrn”;
}
}
This article will focus on MySQL database connection, my upcoming article will cover Oracle database connection.
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.