\$ver, "produ-6ren">
gpt4 book ai didi

mysql - Perl 中的 GetOpt::Long 模块用于默认值

转载 作者:行者123 更新时间:2023-11-29 00:01:34 25 4
gpt4 key购买 nike

我正在编写一个 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;

如果没有给出命令行参数,我需要默认执行这个查询。谢谢。

最佳答案

我刚刚添加了下面这行并且它有效

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;
}

关于mysql - Perl 中的 GetOpt::Long 模块用于默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29571692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com