Perl 과의 연동 프로그래밍을 위해 먼저 관련 펄 rpm 을 설치합니다.
필자의 OS 는 참고로 CentOs 6.3 입니다. 레드햇계열이죠.
[root@olmaster:~]# rpm -qa|grep DBI ## DBI 패키지는 이미 깔려 있네요.
perl-DBI-1.609-4.el6.x86_64
perl-DBIx-Simple-1.32-3.el6.noarch
[root@olmaster:~]# rpm -qa|grep DBD ## perl-DBD-Pg 패키지는 안보이네요. 그럼 깔아야죠
perl-DBD-SQLite-1.27-3.el6.x86_64
perl-DBD-MySQL-4.013-3.el6.x86_64
[root@olmaster:~]# yum install perl-DBD-Pg
Loaded plugins: refresh-packagekit, security
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package perl-DBD-Pg.x86_64 0:2.15.1-4.el6_3 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==================================================================================================
Package Arch Version Repository Size
==================================================================================================
Installing:
perl-DBD-Pg x86_64 2.15.1-4.el6_3 ol6_latest 196 k
Transaction Summary
==================================================================================================
Install 1 Package(s)
Total download size: 196 k
Installed size: 544 k
Is this ok [y/N]: y
Downloading Packages:
perl-DBD-Pg-2.15.1-4.el6_3.x86_64.rpm | 196 kB 00:05
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : perl-DBD-Pg-2.15.1-4.el6_3.x86_64 1/1
Verifying : perl-DBD-Pg-2.15.1-4.el6_3.x86_64 1/1
Installed:
perl-DBD-Pg.x86_64 0:2.15.1-4.el6_3
Complete!
[root@olmaster:~]# rpm -qa|grep DBD
perl-DBD-SQLite-1.27-3.el6.x86_64
perl-DBD-MySQL-4.013-3.el6.x86_64
perl-DBD-Pg-2.15.1-4.el6_3.x86_64 ## 잘 설치되었네요.
-- sample.pl 프로그램입니다.
[postgres@olmaster:~/oradba]$ cat sample.pl
#!/usr/bin/perl
# Copyright (c) PostgresDBA.com
use DBI;
use strict;
my $dbh = DBI->connect("DBI:Pg:dbname=scottdb;host=olmaster", "scott", "tiger", {'RaiseError' => 1});
# execute SELECT query
my $sth = $dbh->prepare("SELECT empno, ename FROM emp");
$sth->execute();
while(my $ref = $sth->fetchrow_hashref()) {
print "$ref->{'empno'} => $ref->{'ename'}\n";
}
$dbh->disconnect();
-- 이제 실행 해볼까요?
[postgres@olmaster:~/oradba]$ chmod 755 sample.pl
[postgres@olmaster:~/oradba]$ ./sample.pl
7369 => SMITH
7499 => ALLEN
7521 => WARD
7566 => JONES
7654 => MARTIN
7698 => BLAKE
7782 => CLARK
7788 => SCOTT
7839 => KING
7844 => TURNER
7876 => ADAMS
7900 => JAMES
7902 => FORD
7934 => MILLER
[postgres@olmaster:~/oradba]$
결과가 잘 나오네요.
아마존 싸이트에서 "Perl DBI" 키워드로 관련 Perl 데이터베이스 프로그래밍 책을 찾아볼수 있습니다.
참고로, 오라클로도 테스트해봤는데, 오라클과도 연동이 잘 된답니다.
펄은 필자가 좋아하는 랭귀지중 하나랍니다.