如何检查Perl中的DBI查询是否返回了多条记录?

时间:2022-06-21 07:14:56

I searched for this on the site already but couldn't locate anything that answered my question. Which brings me to the details:

我已经在网站上搜索了这个,但找不到任何回答我问题的内容。这让我了解详情:

I am querying database using a simple select query, realistically (given the scenario) it should never return more than about 5 to 6 records. I just want to check if more than one has been returned. And if more than one has been return act upon that (in an else statement) if only one has been returned then I progress with the script.

我使用简单的选择查询来查询数据库,实际上(给定方案)它应该永远不会返回超过大约5到6条记录。我只是想检查是否已经返回了多个。如果只有一个已经返回,那么如果只有一个已经返回,那么我会使用该脚本进行操作。


my $sql = qq { SELECT col1, col2, col3
                 FROM table
            WHERE col1 = 'foo'
          };

my $sth = $dbc->prepare_cached($sql) or die "Could not prepare statement: " . $dbc->errstr;

$sth->execute() or die "Could not execute statement: " . $sth->errstr;

# Not sure how to efficiently check for more than one row returned and still progress if true... This is my problem! I'm thinking a nested if to see if any rows were returned, and then progress with the check of one or more rows? But how can this me checked (like a count function?)
if (my $ref = $sth->fetchrow_hashref()) {

 # One row was returned...

} else {
 # More than one row was returned... Uh oh!
}

If you guys know of a thread that I couldn't locate that answers this question simply redirect my and I'll nullify this thread!

如果你们知道一个我找不到的线程回答这个问题,只需重定向我,我就会使这个帖子无效!

Regards, BorisTheBulletDodger!

4 个解决方案

#1


my $sth = $dbh->prepare("SELECT col1, col2, col3 FROM tablename");
$sth->execute();
my $rows = $sth->fetchall_arrayref({});
if (@$rows == 0) {
    die "no rows returned";
} elsif (@$rows > 1) {
    die "too many rows returned";
}
my $row = $rows->[0];
print "col1 is $row->{col1}\n";

#2


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

my $row = $sth->fetchrow_hashref();
my $second = $sth->fetchrow_hashref();

if ($second) {
  print "2+ rows\n";
} elsif ($row) {
  print "1 row\n";
} else {
  print "no rows\n";
}

#3


The only way to authoritatively determine whether more than one row has been found by a SELECT is to try to retrieve more than one row. DBI does provide the ->rows property, but it is only guaranteed to work for non-SELECT statements.

权威地确定SELECT是否找到多行的唯一方法是尝试检索多行。 DBI确实提供了 - > rows属性,但只保证它适用于非SELECT语句。

The answers to How can I check if a database query will return results? may also prove useful, even though it's not quite the same question.

我如何检查数据库查询是否会返回结果的答案?也可能证明有用,即使它不是完全相同的问题。

#4


Use one of the fetchall_* or selectall_* methods (see the DBI documentation), since you'll only have 5-6 at most. Then determine how many rows you got after all.

使用fetchall_ *或selectall_ *方法之一(请参阅DBI文档),因为您最多只有5-6个。然后确定你毕竟有多少行。

#1


my $sth = $dbh->prepare("SELECT col1, col2, col3 FROM tablename");
$sth->execute();
my $rows = $sth->fetchall_arrayref({});
if (@$rows == 0) {
    die "no rows returned";
} elsif (@$rows > 1) {
    die "too many rows returned";
}
my $row = $rows->[0];
print "col1 is $row->{col1}\n";

#2


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

my $row = $sth->fetchrow_hashref();
my $second = $sth->fetchrow_hashref();

if ($second) {
  print "2+ rows\n";
} elsif ($row) {
  print "1 row\n";
} else {
  print "no rows\n";
}

#3


The only way to authoritatively determine whether more than one row has been found by a SELECT is to try to retrieve more than one row. DBI does provide the ->rows property, but it is only guaranteed to work for non-SELECT statements.

权威地确定SELECT是否找到多行的唯一方法是尝试检索多行。 DBI确实提供了 - > rows属性,但只保证它适用于非SELECT语句。

The answers to How can I check if a database query will return results? may also prove useful, even though it's not quite the same question.

我如何检查数据库查询是否会返回结果的答案?也可能证明有用,即使它不是完全相同的问题。

#4


Use one of the fetchall_* or selectall_* methods (see the DBI documentation), since you'll only have 5-6 at most. Then determine how many rows you got after all.

使用fetchall_ *或selectall_ *方法之一(请参阅DBI文档),因为您最多只有5-6个。然后确定你毕竟有多少行。