写得好纠结,目前不知道哪里有问题,还在研究。。
package TDmodule; use DBI; sub new{ $self={}; $self->{username}=undef; $self->{password}=undef; $self->{host}=undef; $self->{database}=undef; $self->{connect_db}=[]; $self->{select_db}=undef; bless $self; return $self; } sub username{ my $self=shift; if(@_){ $self->{username}=shift; } return $self->{username}; } sub password{ my $self=shift; if(@_){ $self->{password}=shift; } return $self->{password}; } sub host{ my $self=shift; if(@_){ $self->{host}=shift; } return $self->{host}; } sub database{ my $self=shift; if(@_){ $self->{database}=shift; } return $self->{database}; } sub connect_db{ my $self=shift; @{$self->{connect_db}}=DBI->connect("DBI:mysql:database=$self->{database};host=$self->{host}","$self->{username}","$self->{password}") or die "No:$! "; @{$self->{connect_db}}->prepare("select * from old_papar"); @{$self->{connect_db}}->execute(); return @{$self->{connect_db}}; } sub select_db{ my $self=shift; if(@_){ $db=@{$self->{connect_db}}; my $select=$db->prepare(@_); $select->execute(); $self{select_db}=$select->fetchrow_hashref(); @{$self->{connect_db}}->disconnect; } return $self{select_db}; } 1;