Wednesday, March 28, 2012

connecting with databases using Perl DBI module

DBI is perl's database independent interface that makes it possible to do database operations. Words of wisdom from CPAN follows.

"The DBI is a database access module for the Perl programming language. It defines a set of methods, variables, and conventions that provide a consistent database interface, independent of the actual database being used.

It is important to remember that the DBI is just an interface. The DBI is a layer of "glue" between an application and one or more database driver modules. It is the driver modules which do most of the real work. The DBI provides a standard interface and framework for the drivers to operate within."

#!"C:\xampp\perl\bin\perl.exe"
use strict;
use warnings;
use DBI;
my $db = DBI -> connect('dbi:mysql:csv_db;host=localhost','root','');
my $query = $db -> prepare(qq{select * from tbl_name});
$query -> execute();
while (my @row = $query -> fetchrow()) {
foreach my $column (@row) {
print "$column\n";
}
}

More about DBI and its functions from CPAN
http://search.cpan.org/~timb/DBI-1.618/DBI.pm

No comments:

Post a Comment