#!/usr/bin/perl use DBI; use CGI::Carp qw(fatalsToBrowser); $conn = &ConnectDB; $tbl = "テーブル名"; print "Content-type: text/html\n\n"; print "CGI-Perl DBI:Pg TEST\n"; print "\n"; print "

DB接続テスト(CGI-Perl DBI:Pg版)

\n"; ##### データ挿入 ##### #$sql1 = "insert into $tbl values('data1','data2','data3')"; #$sth1 = $conn->prepare($sql1); #$ref1 = $sth1 ->execute; ##### データ更新 ##### #$sql2 = "update $tbl set name='new_data' where name='old_data'"; #$sth2 = $conn->prepare($sql2); #$ref2 = $sth2 ->execute; ##### データ削除 ##### #$sql3 = "delete from $tbl where name='data'"; #$sth3 = $conn->prepare($sql3); #$ref3 = $sth3 ->execute; ##### データ表示 ##### $sql = "select * from $tbl"; $sth = $conn->prepare($sql); $ref = $sth ->execute; for($i=0;$i<$ref;$i++){ ($row1,$row2,$row3) = $sth->fetchrow_array(); print "$row1 / $row2 / $row3
\n"; } $sth ->finish; $conn->disconnect; print "$err\n"; exit; sub ConnectDB{ $dbname = 'データベース名'; $host = 'localhost'; $port = 5432; $username = 'ユーザ名'; $password = ''; $conn = DBI->connect("dbi:Pg:dbname=$dbname;host=$host;port=$port", "$username", "$password"); if($conn->errstr != ""){ $err = $conn->errstr; } #$setencoding = $conn->do("set client_encoding='UTF8';"); return($conn); }