PostgreSQL - Perl 介面



安裝

PostgreSQL 可以使用 Perl DBI 模組與 Perl 整合,該模組是 Perl 程式語言的資料庫訪問模組。它定義了一套方法、變數和約定,提供標準的資料庫介面。

以下是安裝 DBI 模組到 Linux/Unix 系統的簡單步驟:

$ 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  ]

DBI 介面 API

以下是重要的 DBI 函式,足以滿足您從 Perl 程式使用 SQLite 資料庫的需求。如果您正在尋找更復雜的應用程式,則可以檢視 Perl DBI 官方文件。

序號 API & 描述
1

DBI→connect($data_source, "userid", "password", \%attr)

建立到請求的 $data_source 的資料庫連線或會話。如果連線成功,則返回資料庫控制代碼物件。

資料來源具有以下形式:**DBI:Pg:dbname=$database;host=127.0.0.1;port=5432** Pg 是 PostgreSQL 驅動程式名稱,testdb 是資料庫的名稱。

2

$dbh→do($sql)

此例程準備並執行單個 SQL 語句。返回受影響的行數,或在出錯時返回 undef。返回值 -1 表示行數未知、不適用或不可用。這裡 $dbh 是 DBI→connect() 呼叫返回的控制代碼。

3

$dbh→prepare($sql)

此例程準備稍後由資料庫引擎執行的語句,並返回對語句控制代碼物件的引用。

4

$sth→execute()

此例程執行執行準備好的語句所需的任何處理。如果發生錯誤,則返回 undef。成功的執行總是返回 true,無論受影響的行數是多少。這裡 $sth 是 $dbh→prepare($sql) 呼叫返回的語句控制代碼。

5

$sth→fetchrow_array()

此例程獲取下一行資料並將其作為包含欄位值的列表返回。NULL 欄位在列表中返回為 undef 值。

6

$DBI::err

這相當於 $h→err,其中 $h 是任何控制代碼型別,例如 $dbh、$sth 或 $drh。這將返回上次呼叫的驅動程式方法的本機資料庫引擎錯誤程式碼。

7

$DBI::errstr

這相當於 $h→errstr,其中 $h 是任何控制代碼型別,例如 $dbh、$sth 或 $drh。這將返回上次呼叫的 DBI 方法的本機資料庫引擎錯誤訊息。

8

$dbh->disconnect()

此例程關閉先前由 DBI→connect() 呼叫開啟的資料庫連線。

連線到資料庫

以下 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 程式將用於在先前建立的資料庫中建立表:

#!/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

INSERT 操作

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

UPDATE 操作

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

DELETE 操作

以下 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
廣告
© . All rights reserved.