GetOpt :: perl中的long模块,用于默认值

时间:2015-04-10 22:35:38

标签: mysql perl dbi

我正在编写一个perl脚本,它将值作为命令行参数,并将其放入指定的变量中并对数据库执行查询。

GetOptions("version=s"      => \$ver,
       "product=s"      => \$prod) 
or die ("Error in input variables\n");

脚本的查询部分如下:

my $sql = qq|select count(*) from software_build where sw_version like ? and finished > '$date' and product like ?|;
my $sth = $dts->prepare($sql);
$sth->execute($ver, $prod) or die $DBI::errstr;

如果用户不包含命令行参数而不应执行此查询,我应该做出哪些更改:

my $sql = qq|select count(*) from software_build where finished > '$date'|;
my $sth = $dts->prepare($sql);
$sth->execute;

如果没有给出命令行参数,我需要通过deafault执行此查询。谢谢。

1 个答案:

答案 0 :(得分:1)

我刚刚添加了以下这一行,它可以正常工作

if ($ver eq ""){
my $sql = qq|select count(*) from software_build where finished < '$date'|;
my $sth = $dts->prepare($sql);
$sth->execute() or die $DBI::errstr;
my $count = $sth->fetchrow_array();
print $count;
}
else{
my $sql = qq|select count(*) from software_build where sw_version like ? and finished > '$date' and product like ?|;
my $sth = $dts->prepare($sql);
$sth->execute($version, $product) or die $DBI::errstr;
my $count = $sth->fetchrow_array();
print $count;
}