www.dba-oracle.com/t_dbi_interface1.htm
부분부분 필요할때만 봤었는데, 메타정보 얻기와, autocommit 설정까지
궁금한 부분에 대해서 샘플까지 모두 나와 있음!!
혹시 몰라 소스 백업!
## END 로 finally 처리 하는부분과 if defined 활용부분
Example 1.
#!/usr/bin/perl -w
use strict;
use DBI;
my $db = DBI->connect( "dbi:Oracle:Local", "scott", "tiger" )
|| die( $DBI::errstr . "\n" );
$db->{AutoCommit} = 0;
$db->{RaiseError} = 1;
$db->{ora_check_sql} = 0;
$db->{RowCacheSize} = 16;
my $SEL = "invalid SQL statement";
my $sth = $db->prepare($SEL);
print "If you see this, parse phase succeeded without a problem.\n";
$sth->execute();
print "If you see this, execute phase succeeded without a problem.\n";
END {
$db->disconnect if defined($db);
}
Example 2.
#!/usr/bin/perl -w use strict; use DBI; my $db = DBI->connect( "dbi:Oracle:Local", "scott", "tiger" ) || die( $DBI::errstr . "\n" ); $db->{AutoCommit} = 0; $db->{RaiseError} = 1; $db->{ora_check_sql} = 0; $db->{RowCacheSize} = 16; my $SEL = "SELECT * FROM EMP"; my $sth = $db->prepare($SEL); $sth->execute(); while ( my @row = $sth->fetchrow_array() ) { foreach (@row) { $_ = "\t" if !defined($_); print "$_\t"; } print "\n"; } END { $db->disconnect if defined($db); }
##파라미터를 이용한 바인딩
Example 3
#!/usr/bin/perl -w use strict; use DBI; my $db = DBI->connect( "dbi:Oracle:Local", "scott", "tiger" ) || die( $DBI::errstr . "\n" ); $db->{AutoCommit} = 0; $db->{RaiseError} = 1; $db->{ora_check_sql} = 0; $db->{RowCacheSize} = 16; my $SEL = "SELECT e.ename,e.job,d.dname,e.sal FROM emp e, dept d WHERE e.deptno=d.deptno AND e.empno=?"; my $sth = $db->prepare($SEL); print "Enter EMPNO:"; my $empno = <STDIN>; chomp($empno); $sth->bind_param( 1, $empno ); $sth->execute(); my ( $ename, $job, $dept, $sal ) = $sth->fetchrow_array(); write if $sth->rows>0; no strict; format STDOUT_TOP = Employee Name Job Department Salary ---------------------------------------------------------------------------------------------------- . format STDOUT = @<<<<<<<<<<< @<<<<<<<<<<<< @<<<<<<<<< @<<<<<<<<< $ename, $job, $dept, $sal . END { $db->disconnect if defined($db); }
Example 5
##오라클 배열 인서트
my @values;
my $INS="INSERT INTO TAB VALUES (:PLACEHOLDER)";
$sth=$db->prepare($INS);
$sth->bind_param_array(":PLACEHOLDER",\@array);
$sth->execute_array( { ArrayTupleStatus => \my @status });