postgresql可以使用perl dbi
模块与perl集成,perl dbi
模块是perl编程语言的数据库访问模块。 它定义了一组提供标准数据库接口的方法,变量和约定。
以下是在linux / unix机器上安装dbi模块的简单步骤:
$ wget http://search.cpan.org/cpan/authors/id/t/ti/timb/dbi-1.625.tar.gz
$ tar xvfz dbi-1.625.tar.gz
$ cd dbi-1.625
$ perl makefile.pl
$ make
$ make install
如果您需要安装dbi的sqlite
驱动程序,那么可以安装如下:
$ wget http://search.cpan.org/cpan/authors/id/t/tu/turnstep/dbd-pg-2.19.3.tar.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 127.0.0.1/32 md5
您可以启动/重新启动postgres
服务器,可使用以下命令运行:
[root@host]# service postgresql restart
stopping postgresql service: [ ok ]
starting postgresql service: [ ok ]
以下perl代码显示如何连接到现有的数据库。 如果数据库不存在,那么它将自动创建,最后将返回一个数据库对象。
#!/usr/bin/perl
use dbi;
use strict;
my $driver = "pg";
my $database = "testdb";
my $dsn = "dbi:$driver:dbname=$database;host=127.0.0.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\n";
现在,我们运行上面的程序来打开数据库:testdb
,如果成功打开数据库,那么它将给出以下消息:
open database successfully
以下perl程序将用于在之前创建的数据库(testdb
)中创建一个表:
#!/usr/bin/perl
use dbi;
use strict;
my $driver = "pg";
my $database = "testdb";
my $dsn = "dbi:$driver:dbname=$database;host=127.0.0.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\n";
my $stmt = qq(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\n";
}
$dbh->disconnect();
当执行上述程序时,它将在testdb
中创建一张company
表,并显示以下消息:
opened database successfully
table created successfully
以下perl程序显示了如何在上述示例中创建的company
表中创建/插入记录:
#!/usr/bin/perl
use dbi;
use strict;
my $driver = "pg";
my $database = "testdb";
my $dsn = "dbi:$driver:dbname=$database;host=127.0.0.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\n";
my $stmt = qq(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 = qq(insert into company (id,name,age,address,salary)
values (2, 'allen', 25, 'texas', 15000.00 ));
$rv = $dbh->do($stmt) or die $dbi::errstr;
$stmt = qq(insert into company (id,name,age,address,salary)
values (3, 'teddy', 23, 'norway', 20000.00 ));
$rv = $dbh->do($stmt) or die $dbi::errstr;
$stmt = qq(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\n";
$dbh->disconnect();
当执行上述程序时,它将在company
表中创建/插入给定的记录,并显示以下两行:
opened database successfully
records created successfully
以下perl程序显示了如何从上述示例中创建的company
表中获取和显示记录:
#!/usr/bin/perl
use dbi;
use strict;
my $driver = "pg";
my $database = "testdb";
my $dsn = "dbi:$driver:dbname=$database;host=127.0.0.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\n";
my $stmt = qq(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] . "\n";
print "name = ". $row[1] ."\n";
print "address = ". $row[2] ."\n";
print "salary = ". $row[3] ."\n\n";
}
print "operation done successfully\n";
$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
perl代码显示了如何使用update
语句来更新指定记录,然后从company
表中获取和显示更新的记录:
#!/usr/bin/perl
use dbi;
use strict;
my $driver = "pg";
my $database = "testdb";
my $dsn = "dbi:$driver:dbname=$database;host=127.0.0.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\n";
my $stmt = qq(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\n";
}
$stmt = qq(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] . "\n";
print "name = ". $row[1] ."\n";
print "address = ". $row[2] ."\n";
print "salary = ". $row[3] ."\n\n";
}
print "operation done successfully\n";
$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
perl代码显示了如何使用delete
语句删除指定记录,然后从company
表中获取并显示剩余的记录:
#!/usr/bin/perl
use dbi;
use strict;
my $driver = "pg";
my $database = "testdb";
my $dsn = "dbi:$driver:dbname=$database;host=127.0.0.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\n";
my $stmt = qq(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\n";
}
$stmt = qq(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] . "\n";
print "name = ". $row[1] ."\n";
print "address = ". $row[2] ."\n";
print "salary = ". $row[3] ."\n\n";
}
print "operation done successfully\n";
$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