我怎样才能让 Perl DBI 的 selectrow_hashref 到 return 每次迭代的新行?
How can I get Perl DBI's selectrow_hashref to return a new row each iteration?
我正在尝试使用 DBI 的 selectrow_hashref
而不是 fetchrow_hashref
以节省几行代码,但它总是一遍又一遍地返回同一行数据。
my $select="SELECT * FROM table";
while (my ($user_ref) = $dbh->selectrow_hashref()) {
# $user_ref is the same each time!
}
当我使用 fetchrow_hashref
时,一切都很好,每次迭代我都会得到新数据。
my $select="SELECT * FROM table";
my $sth = $dbh->prepare($select) || die "prepare: $select: $DBI::errstr";
$sth->execute() || die "execute: $select: $DBI::errstr";
while (my ($user_ref) = $sth->fetchrow_hashref()) {
# works great, new data in $user_ref each iteration
}
求教,我做错了什么? selectrow_hashref
只是为了检索单个记录吗?好像不是这样 in the doc.
Is selectrow_hashref
only intended to retrieve a single record?
是的。
It doesn't seem that way in the doc.
好吧,该文档说:
It returns the first row of data from the statement.
这对我来说似乎很清楚。
您是在寻找 selectall_hashref
吗?
更新:其实我觉得你想要selectall_array
:
my $select='SELECT * FROM table';
foreach my $user_ref ($dbh->selectall_array($select, { Slice => {} })) {
# $user_ref is a hash ref
say $user_ref->{some_column};
}
我正在尝试使用 DBI 的 selectrow_hashref
而不是 fetchrow_hashref
以节省几行代码,但它总是一遍又一遍地返回同一行数据。
my $select="SELECT * FROM table";
while (my ($user_ref) = $dbh->selectrow_hashref()) {
# $user_ref is the same each time!
}
当我使用 fetchrow_hashref
时,一切都很好,每次迭代我都会得到新数据。
my $select="SELECT * FROM table";
my $sth = $dbh->prepare($select) || die "prepare: $select: $DBI::errstr";
$sth->execute() || die "execute: $select: $DBI::errstr";
while (my ($user_ref) = $sth->fetchrow_hashref()) {
# works great, new data in $user_ref each iteration
}
求教,我做错了什么? selectrow_hashref
只是为了检索单个记录吗?好像不是这样 in the doc.
Is
selectrow_hashref
only intended to retrieve a single record?
是的。
It doesn't seem that way in the doc.
好吧,该文档说:
It returns the first row of data from the statement.
这对我来说似乎很清楚。
您是在寻找 selectall_hashref
吗?
更新:其实我觉得你想要selectall_array
:
my $select='SELECT * FROM table';
foreach my $user_ref ($dbh->selectall_array($select, { Slice => {} })) {
# $user_ref is a hash ref
say $user_ref->{some_column};
}