Postgresql 专题
专题目录
您的位置:database > Postgresql专题 > Perl连接PostgreSQL数据库
Perl连接PostgreSQL数据库
作者:--    发布时间:2019-11-20

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连接到postgresql数据库

以下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

select操作

以下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

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册