取付
PerlDBIモジュールはPerlプログラミング言語用のデータベースアクセスモジュール統合です。これはPerlプログラミング言語用のデータベースアクセスモジュール統合です。Perl DBIモジュールは、標準的なデータベースインタフェースを提供する一連のメソッド、変数、規約を定義しています。
以下は、Linux/UnixマシンにDBIモジュールをインストールする簡単な手順です:
$ wget http://..org/CPAN/authors/id/T/TI/TIMB/DBI-...gz
$ tar xvfz DBI-1.625.tar.gz
$ cd DBI-1.625
$ perl Makefile.PL
$ make
$ make install
SQLite用のDBIドライバをインストールする必要がある場合は、以下のようにインストールします:
$ wget http://..org/CPAN/authors/id/T/TU/TURNSTEP/DBD-Pg-....gz
$ tar xvfz DBD-Pg-2.19.3.tar.gz
$ cd DBD-Pg-2.19.3
$ perl Makefile.PL
$ make
$ make install
PerlのPostgreSQLへのインタフェースを使用するには、PostgreSQLのインストールディレクトリにあるpg_hba.confファイルに以下の行を追加してください:
# IPv4 local connections:
host all all .1/32 md5
Postgresサーバーが起動していない場合は、以下のコマンドで起動/再起動できます:
[root@host]# service postgresql restart
Stopping postgresql service: [ OK ]
Starting postgresql service: [ OK ]
DBIインターフェースAPI
以下は、Perlプログラムを使用してSQLiteデータベースを操作するために必要な重要なDBIルーチンです。より複雑なアプリケーションについては、公式の Perl DBI ドキュメントを参照してください。
データベースへの接続
以下のPerlコードは、既存のデータベースに接続する方法を示しています。データベースが存在しない場合、データベースが作成され、最後にデータベースオブジェクトが返されます。
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "Pg";
my $database = "testdb";
my $dsn = "DBI:$driver:dbname=$database;host=.1;port=5432";
my $userid = "postgres";
my $password = "pass123";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })
or die $DBI::errstr;
print "Opened database successfully
";
上記のプログラムを実行してtestdbのデータベースを開いてみましょう:
Open database successfully
#p#
テーブルの作成
以下のPerlプログラムは、以前に作成したデータベースにテーブルを作成します:
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "Pg";
my $database = "testdb";
my $dsn = "DBI:$driver:dbname=$database;host=.1;port=5432";
my $userid = "postgres";
my $password = "pass123";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })
or die $DBI::errstr;
print "Opened database successfully
";
my $stmt = MSN(CREATE TABLE COMPANY
(ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL););
my $rv = $dbh->do($stmt);
if($rv < 0){
print $DBI::errstr;
} else {
print "Table created successfully
";
}
$dbh->disconnect();
上記のプログラムを実行すると、データベースにCOMPANYテーブルcompanyが作成され、以下のメッセージが表示されます:
Opened database successfully
Table created successfully
INSERT操作
上の例の COMPANY テーブルにレコードを作成する方法を示す Perl プログラム:
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "Pg";
my $database = "testdb";
my $dsn = "DBI:$driver:dbname=$database;host=.1;port=5432";
my $userid = "postgres";
my $password = "pass123";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })
or die $DBI::errstr;
print "Opened database successfully
";
my $stmt = MSN(INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Paul', 32, 'California', 20000.00 ));
my $rv = $dbh->do($stmt) or die $DBI::errstr;
$stmt = MSN(INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (2, 'Allen', 25, 'Texas', 15000.00 ));
$rv = $dbh->do($stmt) or die $DBI::errstr;
$stmt = MSN(INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (3, 'Teddy', 23, 'Norway', 20000.00 ));
$rv = $dbh->do($stmt) or die $DBI::errstr;
$stmt = MSN(INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 ););
$rv = $dbh->do($stmt) or die $DBI::errstr;
print "Records created successfully
";
$dbh->disconnect();
上記のプログラムが実行されると、COMPANYテーブルにレコードが作成され、以下の2行が表示されます:
Opened database successfully
Records created successfully
セレクト操作
上記の例で作成した COMPANY テーブルのレコードを取得して表示する方法を示す Perl プログラム:
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "Pg";
my $database = "testdb";
my $dsn = "DBI:$driver:dbname=$database;host=.1;port=5432";
my $userid = "postgres";
my $password = "pass123";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })
or die $DBI::errstr;
print "Opened database successfully
";
my $stmt = MSN(SELECT id, name, address, salary from COMPANY;);
my $sth = $dbh->prepare( $stmt );
my $rv = $sth->execute() or die $DBI::errstr;
if($rv < 0){
print $DBI::errstr;
}
while(my @row = $sth->fetchrow_array()) {
print "ID = ". $row[0] . "
";
print "NAME = ". $row[1] ."
";
print "ADDRESS = ". $row[2] ."
";
print "SALARY = ". $row[3] ."
";
}
print "Operation done successfully
";
$dbh->disconnect();
上記の手順を実行すると、次のような結果が得られます:
Opened database successfully
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 20000
ID = 2
NAME = Allen
ADDRESS = Texas
SALARY = 15000
ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000
ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000
Operation done successfully
#p#
UPDATE操作
Perlのコードでは、UPDATEステートメントを使用して任意のレコードを更新し、COMPANYテーブルから更新されたレコードを取得して表示する方法を示しています:
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "Pg";
my $database = "testdb";
my $dsn = "DBI:$driver:dbname=$database;host=.1;port=5432";
my $userid = "postgres";
my $password = "pass123";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })
or die $DBI::errstr;
print "Opened database successfully
";
my $stmt = MSN(UPDATE COMPANY set SALARY = 25000.00 where ID=1;);
my $rv = $dbh->do($stmt) or die $DBI::errstr;
if( $rv < 0 ){
print $DBI::errstr;
}else{
print "Total number of rows updated : $rv
";
}
$stmt = MSN(SELECT id, name, address, salary from COMPANY;);
my $sth = $dbh->prepare( $stmt );
$rv = $sth->execute() or die $DBI::errstr;
if($rv < 0){
print $DBI::errstr;
}
while(my @row = $sth->fetchrow_array()) {
print "ID = ". $row[0] . "
";
print "NAME = ". $row[1] ."
";
print "ADDRESS = ". $row[2] ."
";
print "SALARY = ". $row[3] ."
";
}
print "Operation done successfully
";
$dbh->disconnect();
上記の手順を実行すると、次のような結果が得られます:
Opened database successfully
Total number of rows updated : 1
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 25000
ID = 2
NAME = Allen
ADDRESS = Texas
SALARY = 15000
ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000
ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000
Operation done successfully
DELETE操作
Perlのコードでは、DELETE文を使ってレコードを削除し、COMPANYテーブルを取得して残りのレコードを表示する方法を示しています:
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "Pg";
my $database = "testdb";
my $dsn = "DBI:$driver:dbname=$database;host=.1;port=5432";
my $userid = "postgres";
my $password = "pass123";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })
or die $DBI::errstr;
print "Opened database successfully
";
my $stmt = MSN(DELETE from COMPANY where ID=2;);
my $rv = $dbh->do($stmt) or die $DBI::errstr;
if( $rv < 0 ){
print $DBI::errstr;
}else{
print "Total number of rows deleted : $rv
";
}
$stmt = MSN(SELECT id, name, address, salary from COMPANY;);
my $sth = $dbh->prepare( $stmt );
$rv = $sth->execute() or die $DBI::errstr;
if($rv < 0){
print $DBI::errstr;
}
while(my @row = $sth->fetchrow_array()) {
print "ID = ". $row[0] . "
";
print "NAME = ". $row[1] ."
";
print "ADDRESS = ". $row[2] ."
";
print "SALARY = ". $row[3] ."
";
}
print "Operation done successfully
";
$dbh->disconnect();
上記の手順を実行すると、以下のような結果が得られます:
Opened database successfully
Total number of rows deleted : 1
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 25000
ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000
ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000
Operation done successfully