加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

使用Perl和MySql,如何检查空结果?

发布时间:2020-12-16 06:18:18 所属栏目:大数据 来源:网络整理
导读:方式过于简单的例子: # Get Some data$query = $db-prepare(qq{ select * from my_table where id = "Some Value"});$query-execute;# Iterate through the resultsif ( *THE QUERY HAS RETURNED A RESULT* ) { print "Here is list of IDs "; while ($quer
方式过于简单的例子:

# Get Some data
$query = $db->prepare(qq{
    select * from my_table where id = "Some Value"
});
$query->execute;

# Iterate through the results
if ( *THE QUERY HAS RETURNED A RESULT* ) {
    print "Here is list of IDs ";
    while ($query_data = $query->fetchrow_hashref) {
        print "$query_data->{id}";
    }
};

在那里寻找“THE QUERY HURN RETURN A A RESULT”的代码.如果可能的话,我想避免在我的SQL中使用count(*),因为这需要“group by”.

解决方法

my $sth = $dbh->prepare($stmt);
$sth->execute();

my $header = 0;
while (my $row = $sth->fetchrow_hashref) {
    print "Here is list of IDs:n" if !$header++;
    print "$row->{id}n";
}

替代方案:

my $sth = $dbh->prepare($stmt);
$sth->execute();

my $row = $sth->fetchrow_hashref;
print "Here is list of IDs:n" if $row;
while ($row) {
    print "$row->{id}n";
    $row = $sth->fetchrow_hashref;
}

更简单的代码以牺牲内存为代价:

my $ids = $dbh->selectcol_arrayref($stmt);

if (@$ids) {
    print "Here is list of IDs:n";
    print "$_n" for @$ids;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读