Accessing database with PERL

This is a sample coding for connection PostGreSQL using PERL DBI:

#! /usr/bin/perl

use DBI;

my $dbh = DBI->connect("DBI:Pg:dbname=akonlabs;host=localhost", "postgres", "postgres", {'RaiseError' => 1});

my $sth = $dbh->prepare("SELECT * FROM table_name");
$sth->execute();

while(my $ref = $sth->fetchrow_hashref()) {
print "$ref->{'name'} $ref->{'phonenumber'}\n";
}

# clean up
$dbh->disconnect();
}

Useful links:
http://www.techrepublic.com/article/retrieve-data-from-a-postgresql-database-using-the-perl-dbi/6039937

Advertisements