Saturday, August 08, 2009

Connecting to the PostgreSQL using Perl

忘れていたのでメモ

% sudo apt-get install libdbi-perl libdbd-pg-perl
% vi connectToPostgreSQL.pl
#!/usr/bin/env perl

use warnings;
use strict;

use DBI;

my $dbh;
my $sth;
my $rc;
my $rows;

$dbh = DBI->connect('DBI:Pg:database=satoshiabe;host=172.16.227.128;port=5432', 'satoshiabe', 'satoshiabe');

if ($dbh) {
$sth = $dbh->prepare("SELECT * FROM table01 WHERE sysid > ?");
$sth->bind_param(1, 3);
$sth->execute;

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

$rows = $dbh->do("INSERT INTO table011 (uid) VALUES ('01ZZZZZZ')");
print $rows . + " rows affected\n";

$dbh->disconnect;
} else {
print "cannot connect to the database";
}

% chmod +x connectToPostgreSQL.pl
% ./connectToPostgreSQL.pl
%

No comments: