#!/usr/bin/perl use Pg; $conn = &ConnectDB; $tbl = "テーブル名"; print "Content-type: text/html\n\n"; print "<html><head><title>CGI-Perl usePg TEST</title>\n"; print "<meta http-equiv='content-type' content='text/html; charset=utf-8'>\n"; print "</head><body><h2>DB接続テスト(CGI-Perl usePg版)</h2>\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<br>\n"; } } print "$errorMessage<br>\n"; print "$err</body></html>\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); }