方式过于简单的例子:
# 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}";
}
};
Run Code Online (Sandbox Code Playgroud)
在那里寻找"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";
}
Run Code Online (Sandbox Code Playgroud)
替代方案:
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;
}
Run Code Online (Sandbox Code Playgroud)
更简单的代码以牺牲内存为代价:
my $ids = $dbh->selectcol_arrayref($stmt);
if (@$ids) {
print "Here is list of IDs:\n";
print "$_\n" for @$ids;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5089 次 |
最近记录: |