#!/usr/bin/perl
use Pg;
$conn = &ConnectDB;
$tbl = "テーブル名";
print "Content-type: text/html\n\n";
print "
CGI-Perl usePg TEST\n";
print "\n";
print "DB接続テスト(CGI-Perl usePg版)
\n";
##### データ挿入 #####
#$sql1 = "insert into $tbl values('data1','data2','data3')";
#$sth1 = $conn->exec($sql1);
##### データ更新 #####
#$sql2 = "update $tbl set name='new_data' where name='old_data'";
#$sth2 = $conn->exec($sql2);
##### データ削除 #####
#$sql3 = "delete from $tbl where name='data'";
#$sth3 = $conn->exec($sql3);
##### データ表示 #####
$sql = "select * from $tbl";
$select = $conn ->exec($sql);
$check = $select->ntuples;
if($check != 0){
for ($i=0;$i<$check;$i++){
$row1 = $select->getvalue($i,0);
$row2 = $select->getvalue($i,1);
$row3 = $select->getvalue($i,2);
print "$row1 / $row2 / $row3
\n";
}
}
print "$errorMessage
\n";
print "$err\n";
exit;
sub ConnectDB{
$pghost = "localhost";
$pgport = "5432";
$pgoptions = "";
$pgtty = "";
$pgdbname = "データベース名";
$pguser = "ユーザ名";
$pgpwd = "パスワード";
my($conn);
$conn = Pg::setdbLogin($pghost, $pgport, $pgoptions, $pgtty, $pgdbname, $pguser, $pgpwd);
if( $conn->status == PGRES_CONNECTION_BAD){
$err = "接続失敗";
$errorMessage = $conn->errorMessage
}
#$setencoding = $conn->exec("set client_encoding='UTF8';");
return($conn);
}