我想从我的数据库中检索一些数据,如果$end_time1为空,那么将是update table,否则将不做任何事情,但是当我运行代码时,我发现$end_time1为空,那么将是update table,但是如果不为空,则将返回错误:
Use of uninitialized value $end_time1 in string eq ........
部分代码:
my $select_sth = $dbh->prepare("SELECT id ,H1, H2, addr1, addr2, time_1, time_2,
end_time_1,end_time_2,count1,count2 FROM service") or die "$dbh->errstr";
$select_sth->execute() or die "$dbh->errstr";
while (my @row_ref = $select_sth->fetchrow_array)
{
my $Rid = $row_ref[0];
my $addr1 = $row_ref[3];
my $addr2 = $row_ref[4];
my $end_time1 = "NULL" unless $row_ref[7];
my $end_time2 = "NULL" unless $row_ref[8];
my $count1 = $row_ref[9];
my $count2 = $row_ref[10];
if($end_time1 eq "NULL")
{
print "$end_time1 is null\n";
$dbh->do ("update service set end_time_1 = '$datetime_now' where id = $Rid");
}
}
拜托,我的密码怎么了?如何修复?
最佳答案
当前代码仅在未定义$endtime1
时设置$row_ref[7]
。这意味着如果$endtime1
有一个值,则$row_ref[7]
是未定义的,因此在测试时会得到Use of uninitialized value $end_time1 in string eq...
错误。
更改代码,以便将$endtime1
设置为$row_ref[7]
(如果已定义)或NULL
:
my $end_time1 = $row_ref[7] || 'NULL';
然后可以使用现有代码:
if ($end_time1 eq "NULL")
{
print "end_time1 is null\n";
$dbh->do ("update service set end_time_1 = '$datetime_now' where id = $Rid");
}
同样的问题也存在于
$endtime2
,所以你可能想做出类似的改变。