Perl 基于OOP的数据库链接(增删改查)
package TDmodule; use DBI; sub new{ $self={}; $self->{select_db}=undef; $self->{delete_db}=undef; $self->{insert_db}=undef; $self->{update_db}=undef; bless $self; return $self; } sub connect_db{ my $self=shift; if(@_){ $self->{'connect_db'}=DBI->connect("DBI:mysql:database=@_[0];host=@_[1]","@_[2]","@_[3]") or die "No:$!\n"; } return $self->{'connect_db'}; } sub select_db{ my $self=shift; if(@_){ my $select=@_[0]->prepare(@_[1]); $select->execute(); $self{select_db}=$select; } return $self{select_db}; @_[0]->disconnect; } sub update_db{ my $self=shift; if(@_){ eval{ $self->{update_db}=@_[0]->do(@_[1]); } or die "No\n"; } } sub insert_db{ my $self=shift; if(@_){ eval{ $self->{insert_db}=@_[0]->do(@_[1]); } or die "No\n"; } } sub delete_db{ my $self=shift; if(@_){ eval{ $self->{delete_db}=@_[0]->do(@_[1]); } or die "No\n"; } } 1;