Perlのデータベース接続PostgreSQL
2025年5月11日
rootユーザになり以下のコマンドでPostgreSQLをアクセスするためのPerlモジュールをインストールすることができます。
install perl-DBI perl-DBD-Pg
PostgreSQLに接続し現在の日付を表示するサンプルコード
#!/usr/bin/perl
use DBI;
# PostgreSQL
our $DB_NAME = “postgres”;
our $DB_USER = “sakura”;
our $DB_PASS = “sakura”;
our $DB_HOST = “127.0.0.1”;
our $DB_PORT = “5432”;
my $dbh = DBI->connect(“dbi:Pg:dbname=$DB_NAME;host=$DB_HOST;port=$DB_PORT”,”$DB_USER”,”$DB_PASS”) or die “$!\n Error: failed to connect to DB.\n”;
my $sth = $dbh->prepare(“SELECT now();”);
$sth->execute();
while (my $ary_ref = $sth->fetchrow_arrayref) {
my ($row) = @$ary_ref;
print $row , “\n”;
}
$sth->finish;
$dbh->disconnect;
1;
Last Updated on 2025年5月11日1:57 pm by cgishop
前