ソフト開発塾

DBI における接続
# 
use DBI;
$dbh = DBI->connect('dbi:Pg:', '', '');
接続失敗と成功
サンプル プログラム (dbitest.pl)
#!/usr/bin/perl

use DBI;

	print '----- 0 -------------------------------------------------------'."\n";
	$dbh = DBI->connect('dbi:Pg:', '', '');
	print '----- 1 -------------------------------------------------------'."\n";
	$dbh = DBI->connect('dbi:Pg:host=/tmp/.s.PGSQL.5432', '', '');
	print '----- 2 -------------------------------------------------------'."\n";
	$dbh = DBI->connect('dbi:Pg:host=/tmp', '', '');
	print '----- 3 -------------------------------------------------------'."\n";
	$dbh = DBI->connect('dbi:Pg:host=/tmp dbname=test', '', '');
	print '----- 4 -------------------------------------------------------'."\n";
	$dbh = DBI->connect('dbi:Pg:dbname=test host=/tmp user=no', '', '');
	print '----- 5 -------------------------------------------------------'."\n";
	$dbh = DBI->connect('dbi:Pg:dbname=test host=/tmp', 'no', '');
	print '----- 6 -------------------------------------------------------'."\n";
	$dbh = DBI->connect('dbi:Pg:dbname=test host=/tmp', 'nobody', '');
	if($dbh){
		$sth=$dbh->prepare('select count(*) as "ct"  from pg_stat_activity;');
		if($sth->err){
			print 'sth err:'.$sth->errstr."\n";
		}else{
			$Result=$sth->execute();
			while(@mm_vals=$sth->fetchrow()){
				print " ct = [". $mm_vals[0]."]\n";
			}
			$sth->finish();
		}
		$dbh->disconnect();
	}
実行結果
# ./dbitest.pl
----- 0 -------------------------------------------------------
DBI connect('','',...) failed: サーバに接続できませんでした: そのようなファイルやディレクトリはありません
                              ローカルにサーバが稼動していますか?
                              Unixドメインソケット"/var/run/postgresql/.s.PGSQL.5432"で通信を受け付けていますか? at ./dbitest.pl line 6.
----- 1 -------------------------------------------------------
DBI connect('host=/tmp/.s.PGSQL.5432','',...) failed: サーバに接続できませんでした: ディレクトリではありません
                              ローカルにサーバが稼動していますか?
                              Unixドメインソケット"/tmp/.s.PGSQL.5432/.s.PGSQL.5432"で通信を受け付けていますか? at ./dbitest.pl line 8.
----- 2 -------------------------------------------------------
DBI connect('host=/tmp','',...) failed: FATAL:  no pg_hba.conf entry for host "[local]", user "root", database "root", no encryption at ./dbitest.pl line 10.
----- 3 -------------------------------------------------------
DBI connect('host=/tmp dbname=test','',...) failed: FATAL:  no pg_hba.conf entry for host "[local]", user "root", database "test", no encryption at ./dbitest.pl line 12.
----- 4 -------------------------------------------------------
DBI connect('dbname=test host=/tmp user=no','',...) failed: FATAL:  no pg_hba.conf entry for host "[local]", user "no", database "test", no encryption at ./dbitest.pl line 14.
----- 5 -------------------------------------------------------
DBI connect('dbname=test host=/tmp','no',...) failed: FATAL:  no pg_hba.conf entry for host "[local]", user "no", database "test", no encryption at ./dbitest.pl line 16.
----- 6 -------------------------------------------------------
 ct = [6]
ちなみに、
$dbh = DBI->connect('dbi:Pg:', '', '') || die 'err:'.$dbh->err.' ['.$dbh->errstr."]\n";
の実行結果は
DBI connect('','',...) failed: サーバに接続できませんでした: そのようなファイルやディレクトリはありません
                              ローカルにサーバが稼動していますか?
                              Unixドメインソケット"/var/run/postgresql/.s.PGSQL.5432"で通信を受け付けていますか? at ./dbitest.pl line 33.
err: []
となり、このコードはダメダメである
connectが失敗しているため、$dbhにはまともな値が入っていない。
そのため、$dbh->err や $dbh->errstr を表示できるわけがない。
ソフト開発塾 前のページへ戻る

--- KONDO-NET.GR.JP ---
今日のアクセス人目