我想使用 perl CGI 从数据库中删除数据,但它向我显示了错误:全局符号“$queryDelete”需要在 Dbm.pm 第 10 行显示明确的包名称。
package Dbm;
use DBI;
use strict;
use POSIX "fmod";
sub connection {
return DBI->connect("DBI:mysql:boys:127.0.0.1" ,'root','');
};
sub do_delete {
my $rData = shift;
$queryDelete = " WHERE ";
my $cWhere = get_condition($rData);
my $queryDelete="DELETE FROM $rData->{table} $cWhere";
my $dbh = connection;
my $sqlDelete = $dbh->prepare($queryDelete) or die "Can't prepare $queryDelete: $dbh->errstr\n";
return ($sqlDelete->execute) or die "can't execute the query: $sqlDelete->errstr";
}
sub get_condition{
my $rData = shift;
my $cWhere = " 1=1 ";
while (my ($key,$value) = each $rData->{condition}) {
if (ref($value) eq 'ARRAY'){
$cWhere .= " AND ";
#print $key.$value->[0];
my $counter=0;
foreach my $cValueArray (@{$value}){
if(fmod($counter,3)==0) {
$cWhere.=" ".$key." ".$cValueArray." ";
}
else {
$cWhere.= " ".$cValueArray. " ";
}
$counter++;
}
}else{
#print "$key=$value\n
$cWhere .= " AND ". $key."=". $value;
}
};
return $cWhere;
}
1;
测试.pl
my $q = new CGI();
my $handle = Dbm::connection();
sub test_delete{
my $rData = {
table =>'boys',
condition => {
ID => 99,
Emri => ['<>','\'mon\'','AND','like','\'boy%\''],
}
};
return Dbm::do_delete($rData);
};
print $q-> start_html(
-title => "Main",
-style => [ {-src =>'/media/css/ui-lightness/jquery-ui-1.10.3.custom.css', -rel=>'alternate'},
{-src =>'/media/css/ui-lightness/mystyle.css', -rel=>'stylesheet', -type=>'text/css'}
],
-script => [
{ -src=>'/media/js/jquery-1.9.1.js'},
{ -src=>'/media/js/jquery-ui-1.10.3.custom.js' }
]
);
print test_delete();