连接数据库测试
本例中展示的为本地连接数据库,如远程连接,需要修改配置文件pg_hba.conf或postgresql.conf以完成远程连接数据库的配置。
连接数据库
以下Perl代码显示如何连接到现有的数据库,并返回一个数据库对象。
# shell
vim connect.pl
# perl
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "Pg";
my $database = "vastbase";
my $dsn = "DBI:$driver:dbname=$database;host=127.0.0.1;port=5432";
my $userid = "test";
my $password = "Aa123456";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })
or die $DBI::errstr;
print "Opened database successfully\n";
# shell
perl connect.pl
脚本成功执行输出如下:
[root@localhost ~]# perl connect.pl
NOTICE: Last successful login info
login time:2021-08-09 03:26:36
application name:
Ip address:localhost
method:MD5
Failed 0 times since last successful login
Opened database successfully
创建表
以下Perl程序将用于在之前连接的数据库Vastbase G100中创建一个表:
# shell
vim createtbl.pl
# perl
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "Pg";
my $database = "vastbase";
my $dsn = "DBI:$driver:dbname=$database;host=127.0.0.1;port=5432";
my $userid = "test";
my $password = "Aa123456";
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();
# shell
perl createtbl.pl
执行上述程序后,它将在Vastbase G100中创建一张company表:
脚本成功执行输出如下:[root@localhost ~]# perl createtbl.pl
NOTICE: Last successful login info
login time:2021-08-09 03:26:51
application name:
Ip address:localhost
method:MD5
Failed 0 times since last successful login
Opened database successfully
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "company_pkey" for table "company"
Table created successfully
INSERT操作
以下Perl程序显示了如何在上述示例中创建的COMPANY表中创建/插入记录:
# shell
vim insert.pl
# perl
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "Pg";
my $database = "vastbase";
my $dsn = "DBI:$driver:dbname=$database;host=127.0.0.1;port=5432";
my $userid = "test";
my $password = "Aa123456";
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();
# shell
perl insert.pl
执行以上程序后,它将在company表中插入给定的记录:
脚本成功执行输出如下:[root@localhost ~]# perl insert.pl
NOTICE: Last successful login info
login time:2021-08-09 03:27:27
application name:
Ip address:localhost
method:MD5
Failed 0 times since last successful login
Opened database successfully
Records created successfully
SELECT操作
以下Perl程序显示了如何从上述示例中创建的COMPANY表中获取和显示记录:
# shell
vim select.pl
# perl
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "Pg";
my $database = "vastbase";
my $dsn = "DBI:$driver:dbname=$database;host=127.0.0.1;port=5432";
my $userid = "test";
my $password = "Aa123456";
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();
# shell
perl select.pl
脚本成功执行输出如下:
[root@localhost ~]# perl select.pl
NOTICE: Last successful login info
login time:2021-08-09 03:30:35
application name:
Ip address:localhost
method:MD5
Failed 0 times since last successful login
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表中获取和显示更新的记录:
# shell
vim update.pl
# perl
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "Pg";
my $database = "vastbase";
my $dsn = "DBI:$driver:dbname=$database;host=127.0.0.1;port=5432";
my $userid = "test";
my $password = "Aa123456";
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();
# shell
perl update.pl
脚本成功执行输出如下:
[root@localhost ~]# perl update.pl
NOTICE: Last successful login info
login time:2021-08-09 03:32:02
application name:
Ip address:localhost
method:MD5
Failed 0 times since last successful login
Opened database successfully
Total number of rows updated : 1
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
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 25000
Operation done successfully
DELETE操作
Perl代码显示了如何使用DELETE语句删除指定记录,然后从COMPANY表中获取并显示剩余的记录:
# shell
vim delete.pl
# perl
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "Pg";
my $database = "vastbase";
my $dsn = "DBI:$driver:dbname=$database;host=127.0.0.1;port=5432";
my $userid = "test";
my $password = "Aa123456";
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();
# shell
perl delete.pl
脚本成功执行输出如下:
[root@localhost ~]# perl delete.pl
NOTICE: Last successful login info
login time:2021-08-09 03:33:16
application name:
Ip address:localhost
method:MD5
Failed 0 times since last successful login
Opened database successfully
Total number of rows deleted : 1
ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000
ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 25000
Operation done successfully