gpt4 book ai didi

perl - 确定$ sth是否有行而不消耗它?

转载 作者:行者123 更新时间:2023-12-04 13:53:37 28 4
gpt4 key购买 nike

使用DBI是否可以确定SELECT语句的已执行语句句柄是否返回任何行而不从中获取行?

IE。就像是:

use DBI;
...
my $sth = $dbh->prepare("SELECT ...");
$sth->execute;

if (..$sth will return at least one row...) {
...
} else {
...
}

而且我想执行测试 $sth will return at least one row而不对 $sth执行任何提取方法。

注意-我不需要确切的行数(即 $sth->rows),我只需要知道 $sth->rows是否会> 0。

最佳答案

$ sth-> rows仍然是您的最佳选择。如您所说,只需检查它是否大于0。

if ($sth->rows > 0){
# magic here!
}

除了! DBI文档说,这对于选择语句是不可靠的,直到所有行都已被获取。无法确定要预先获取多少行的方法。如果您需要了解此建议,则建议先做一个
select count(*) from <table>

参见 this section of the DBI documentation

关于perl - 确定$ sth是否有行而不消耗它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8918992/

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