俺だけのために Ore! Tips!

ActivePerl から Oracle にアクセスする

2005年12月 7日 作業


IIS 上で実行される CGI から Oracle DB にアクセスするという案件の見積もり依頼があったので、Windows2000 Professional 上の ActivePerl から別サーバ上の Oracle にアクセス出来るか実験してみた。

まず、PPM を使って DBI+DBD::Oracle モジュールをインストール。(コマンドプロンプトから)

Microsoft Windows 2000 [Version 5.00.2195]
(C) Copyright 1985-2000 Microsoft Corp.

C:\Documents and Settings\shinoda>ppm
PPM - Programmer's Package Manager version 3.2.
Copyright (c) 2001 ActiveState Corp. All Rights Reserved.
ActiveState is a division of Sophos.

Entering interactive shell. Using Term::ReadLine::Perl as readline library.

Type 'help' to get started.

ppm> install DBD-ORACLE
====================
Install 'DBI' version 1.49 in ActivePerl 5.8.7.813.
====================
Downloaded 556633 bytes.
Extracting 73/73: blib/arch/auto/DBI/Driver_xst.h
Installing C:\usr\local\Perl\site\lib\auto\DBI\dbd_xsh.h
Installing C:\usr\local\Perl\site\lib\auto\DBI\DBI.bs
<略>
Installing C:\usr\local\Perl\bin\dbiproxy
Installing C:\usr\local\Perl\bin\dbiproxy.bat
Successfully installed DBI version 1.49 in ActivePerl 5.8.7.813.
====================
Install 'DBD-ORACLE' version 1.16 in ActivePerl 5.8.7.813.
====================
Downloaded 214134 bytes.
Extracting 36/36: blib/script/ora_explain.bat
Installing C:\usr\local\Perl\site\lib\auto\DBD\Oracle\dbdimp.h
Installing C:\usr\local\Perl\site\lib\auto\DBD\Oracle\mk.pm
<略>
Installing C:\usr\local\Perl\bin\ora_explain
Installing C:\usr\local\Perl\bin\ora_explain.bat
ORACLE TECHNOLOGY NETWORK
DEVELOPMENT AND DISTRIBUTION LICENSE AGREEMENT

"We," "us," and "our" refers to Oracle USA, Inc. "You" and "your" refers to the individual or entity that wishes to use the Programs from Oracle under this Agreement. "Programs" refers to the Software Products referenced below that you wish to download and use and Program documentation. "License" refers to your right to use the Programs and Program documentation under the terms of this Agreement. The substantive and procedural laws of California govern this Agreement. You and Oracle agree to submit to the exclusive jurisdiction of, and venue in, the courts of San Francisco, San Mateo, or Santa Clara counties in California in any dispute arising out of or relating to this Agreement.
<略>
Entire Agreement
You agree that this Agreement is the complete agreement for the Programs and licenses, and this Agreement supersedes all prior or contemporaneous Agreements orrepresentations. If any term of this Agreement is found to be invalid or unenfo rceable, the remaining provisions will remain effective.

Last updated: 2/16/04
Do you accept the terms of this license ? (y/n) :y
Fetching oci.dll (184 Kb)
<略>
oraociei10.dll checksum: OK
Successfully installed DBD-ORACLE version 1.16 in ActivePerl 5.8.7.813.
ppm>
ppm> exit


これで使えるはず。
下のようなソースを実行してみる。

use DBI;

$host   = '192.168.0.166';
$system_id  = 'test';
$username   = 'hogehoge';
$password   = 'pocochin';

$dbh = DBI->connect("dbi:Oracle:host=$host;sid=$system_id", $username, $password, {RaiseError=>1, PrintError=>1, AutoCommit=>0});

$csr = $dbh->prepare("SELECT * FROM otoko");
$csr->execute();

$fields = $csr->{NUM_OF_FIELDS};
for ($i=0; $i<$fields; $i++) {
    print ($csr->{NAME}->[$i]);
    print "\n";
}

while(@ary = $csr->fetchrow()){
    foreach $data(@ary) {
        print "$data\n";
    }
}

$csr->finish();
$dbh->disconnect;

# END


コマンドプロンプト上で実行。

D:\>perl oracle_test.pl
ID
NAME
1
???
2
??
3
TAKEMASA

うん。ちゃんとアクセス出来たみたいね。
(但し、日本語がちゃんと出てない。???は「士農田」だし、?? は「小川」。ASCII 文字列の TAKEMASA はちゃんと出てるなあ。ま、おいおい調査しましょう)

ちなみに、接続した Oracle は、Windows 2000 Professional 上で動いている Oracle8i Workgroup Server。Ver は 8.1.7 でありんす。


前ページに戻る


Copyright (C) 2005 S.Maaasamasa.