连接数据库测试
本例中展示的为本地连接数据库,如远程连接,需要修改配置文件atlasdb_hba.conf或atlasdb.conf以完成远程连接数据库的配置。
连接数据库
以下Perl代码显示如何连接到现有的数据库。如果数据库不存在,那么它将自动创建,最后将返回一个数据库对象。
# shell
vim connect.pl
# perl
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "Pg";
my $database = "atlasdb";
my $dsn = "DBI:$driver:dbname=$database;host=127.0.0.1;port=15432";
my $userid = "atlasdb";
my $password = "atlasdb";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })
or die $DBI::errstr;
print "Opened database successfully\n";
# shell
perl connect.pl
如果成功打开数据库,将打印如下消息:
Opened database successfully
创建表
以下Perl程序将用于在之前创建的数据库Vastbase E100中创建一个表:
# shell
vim createtbl.pl
# perl
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "Pg";
my $database = "atlasdb";
my $dsn = "DBI:$driver:dbname=$database;host=127.0.0.1;port=15432";
my $userid = "atlasdb";
my $password = "atlasdb";
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
执行上述程序后,它将在atalsdb中创建一张company表:
并显示如下消息:
Opened database successfully
Table created successfully
INSERT操作
以下Perl程序显示了如何在上述示例中创建的COMPANY表中创建/插入记录:
# shell
vim insert.pl
# perl
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "Pg";
my $database = "atlasdb";
my $dsn = "DBI:$driver:dbname=$database;host=127.0.0.1;port=15432";
my $userid = "atlasdb";
my $password = "atlasdb";
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表中插入给定的记录:
并显示以下两行:
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 = "atlasdb";
my $dsn = "DBI:$driver:dbname=$database;host=127.0.0.1;port=15432";
my $userid = "atlasdb";
my $password = "atlasdb";
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
执行上述程序后,会产生以下结果:
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 = "atlasdb";
my $dsn = "DBI:$driver:dbname=$database;host=127.0.0.1;port=15432";
my $userid = "atlasdb";
my $password = "atlasdb";
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
执行上述程序后,会产生以下结果:
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 update.pl
# perl
#!/usr/bin/perl
use DBI;
use strict;
my $driver = "Pg";
my $database = "atlasdb";
my $dsn = "DBI:$driver:dbname=$database;host=127.0.0.1;port=15432";
my $userid = "atlasdb";
my $password = "atlasdb";
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
执行上述程序后,会产生以下结果:
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