我正在研究Perl的DBI,并看到以下方法:
selectrow_array
selectrow_arrayref
selectrow_hashref
所有这些方法都从结果集中返回1行。因此,我不明白它们的用处是什么。它们是用来替代使用
selectrow_array
selectrow_arrayref
selectrow_hashref
所有这些方法都从结果集中返回1行。因此,我不明白它们的用处是什么。它们是用来替代使用
LIMIT 1
的吗?LIMIT 1
的吗?有很多例子可以说明只获取一行是很有用的,这些函数简化了搜索过程。例如,获取结果集的计数
my $sql = "select count(*) from people where age>?";
my ($count) = $dbh->selectrow_array($sql, undef, 42);
替代方案需要执行以下步骤:准备、绑定和执行、提取结果和结束。
是的,这只是一种语法糖,可以使程序更小,当你只需要选择一行时。正如手册所说:
selectrow_hashref - 这个实用程序方法组合了 "prepare"、"execute" 和 "fetchrow_hashref"
因此,与其写:
$sql = qq{
SELECT
project.domain,
project.pages_to_save,
project.pages_to_check,
IFNULL(project.no_exact_result, 0) AS no_exact_result
FROM
project
WHERE
project.id=?
};
my $sth = $dbh->prepare($sql);
$sth->execute($project_id);
my $values_ref = $sth->fetchrow_hashref();
$sth->finish();
您可以编写:
$sql = qq{
SELECT
project.domain,
project.pages_to_save,
project.pages_to_check,
IFNULL(project.no_exact_result, 0) AS no_exact_result
FROM
project
WHERE
project.id=?
};
my $values_ref = $sth->selectrow_hashref($sql, undef, $project_id);
project.id=?
会返回1行? - Jim