Perl 数据库模块的封装和调用
2020-08-24 本文已影响0人
木白0101
在项目开发的过程中,也经常遇到perl程序操作数据库的需求。
Perl 5 提供了 DBI 模块来连接和操作数据库。
但是需要怎样编写和封装才能写出简洁高效的数据模块,是需要思考的一个问题。
下面提供一个数据库模块封装的实例,以供参考。
1.编译安装DBI模块。(在这里省略)
2.封装数据库模块,以MYSQL为例。
##############################################################
#PerlDBI.pm
#mysql数据库连接和操作模块
#日期:2020/08/24
#author:木白0101
#############################################################
package PerlDBI;
use strict;
use Encode;
use DBI;
use Constants;
######time###############
our($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time());
$year=1900+$year;
$mon=$mon+1;
our $logtime="$year-$mon-$mday $hour:$min:$sec\t";
our $audit=0;
##########构造函数###########
sub new {
my $class = shift; # 存储包的名字
my $self = {}; # 指向一个空的匿名散列
bless $self, ref($class) || $class; #bless散列到当前包
my $username=Constants::MYSQL_USERNAME; #db_username
my $password=Constants::MYSQL_PASSWORD; #db_password
my $host=Constants::MYSQL_HOST; #db_host
my $dbname=Constants::MYSQL_DATABASE;
my $dbh = DBI->connect("DBI:mysql:$dbname;$host", $username, $password,{mysql_auto_reconnect => 1}) or logger($self->{_dbh}->errstr," ");
$dbh->do("SET character_set_client = 'utf8'");
$dbh->do("SET character_set_connection = 'utf8'");
$dbh->do("SET character_set_results= 'utf8'");
$dbh->do("SET NAMES 'utf8'");
$dbh->{AutoCommit} = 0;
$self->{_dbh}=$dbh;
return $self;
}
############查询函数返回多行记录###########
#参数:sql
#返回:hash数组
############查询函数返回多行记录###########
sub func_select_rows
{
my $self = shift;
my $sql = shift; #参数sql
my @result;
my $sth = $self->{_dbh}->prepare($sql) or logger($self->{_dbh}->errstr,$sql);
$sth->execute() or logger($self->{_dbh}->errstr,$sql);
while (my $rowref = $sth->fetchrow_hashref()) {
push(@result,$rowref);
}
$sth->finish();
return @result;
}
############查询函数返回一行记录###########
#参数:sql
#返回:hash
############查询函数返回一行记录###########
sub func_select_one
{
my $self = shift;
my $sql = shift; #参数sql
my $sth = $self->{_dbh}->prepare($sql) or logger($self->{_dbh}->errstr,$sql);
$sth->execute() or logger($self->{_dbh}->errstr,$sql);
my $rowref = $sth->fetchrow_hashref();
$sth->finish();
return $rowref;
}
############插入、更新、删除函数###########
#参数:sql
#返回:rows 影响条数
############插入、更新、删除函数###########
sub func_do
{
my $self = shift;
my $sql = shift; #sql参数
my $rows = $self->{_dbh}->do($sql) or logger($self->{_dbh}->errstr,$sql);
return $rows;
}
sub quote
{
my $self = shift;
my $param = shift;
return $self->{_dbh}->quote($param);
}
###########COMMIT###########
sub commit
{
my($self) = @_;
$self->{_dbh}->commit() or die $self->{_dbh}->errstr;
}
###########ROLLBACK#########
sub rollback
{
my($self) = @_;
$self->{_dbh}->rollback() or die $self->{_dbh}->errstr;
}
###########析构函数,断开DB连接###########
sub disconnect
{
my($self) = @_;
$self->{_dbh}->disconnect() or die $self->{_dbh}->errstr;
}
sub logger
{
my($msg,$sql) = @_;
my $errpath=Constants::LIB_DIR."/errsql.log";
open OUT,">>$errpath" or die $!;
print OUT $logtime."\t".$msg."\t".$sql."\n";
close OUT;
}
1;
3.程序调用数据库模块
##############################################################
#
#auto_db_access.pl
#
#数据库操作示例
#日期:2020/08/24
#author:木白0101
#############################################################
use warnings;
use strict;
use Data::Dumper;
use PerlDBI; #引用数据库模块
my $dbi = new PerlDBI;
#查询多行
my $site_sql = "select chr,sta_pos,end_pos from read_range";
my @hash_site_result = $dbi->func_select_rows($site_sql);
my $regions = "";
foreach my $read_range(@hash_site_result){
my $chr_read = $read_range->{"chr"};
my $sta_pos = $read_range->{"sta_pos"};
my $end_pos = $read_range->{"end_pos"};
}
#查询单行
my $user_info = $dbi->func_select_one("select sample_no,sex,birth_date from user_info where sample_no like '%".$sample_no."%'");
my $gender = $user_info->{"sex"};
#数据库操作
my $insert_sql = "insert into user_info(sample_no,sex,birth_date)values('test','male','2020-08-24')";
my $rows = $dbi->func_do($insert_sql);
$dbi->commit();
#关闭连接
$dbi->disconnect();