PerlからDBを使う
DBIモジュールなんて意識したことなかったのですが、調べる機会があったのでメモしておきます。
createdb test psql test create table test (id int, name text, date timestamp); insert into table (id, name, date) values (1, 'hoge', now()); insert into table (id, name, date) values (2, 'fuga', now());
#!/usr/bin/perl
use DBI;
use strict;
&main();
sub main {
my $connection = DBI->connect('dbi:Pg:dbname=test', 'hoge', 'pass')
or die 'cannot connet';
my $result = $connection->prepare('select * from test')
or die 'sql error';
$result->execute() or die 'cannot execute';
while (my @array = $result->fetchrow_array()) {
printf ("%s,%s,%s\n", $array[0], $array[1], $array[2]);
}
$connection->disconnect();
}