Perl 的 SQLite3:{NAME} 不工作?
以下是我正在开发的 sqlite 数据库应用程序中的一段代码:
my $query = "select * from pins";
my $sth = $dbh->prepare($query) or die "Couldn't prep: $DBI::errstr";
$sth->execute or die "Exec problem: $DBI::errstr";
my $result = $sth->fetchall_arrayref();
my $names = $sth->{NAME} or die "Name failed: $DBI::errstr";
foreach my $row (@$res) {
# ... do some row-specific things
foreach my $cell (@$row) {
# ... do some cell-specific things
}
}
查询运行得很好,实际上它返回了正确的结果。然而,由于某种原因,这条线
my $names = $sth->{NAME} or die "Name failed: $DBI::errstr";
失败了。 {NAME} 永远不会返回我期望的 arrayref 。如果我去掉 die 子句,它就可以正常运行(当然,无论我在哪里使用 $names,都会抛出预期的“使用未初始化值”警告)。
鉴于查询运行良好,是否有一些明显的原因导致我错过了 {NAME} 不会启动的情况?
谢谢!
Here's a snippet of code from an sqlite database application I'm working on:
my $query = "select * from pins";
my $sth = $dbh->prepare($query) or die "Couldn't prep: $DBI::errstr";
$sth->execute or die "Exec problem: $DBI::errstr";
my $result = $sth->fetchall_arrayref();
my $names = $sth->{NAME} or die "Name failed: $DBI::errstr";
foreach my $row (@$res) {
# ... do some row-specific things
foreach my $cell (@$row) {
# ... do some cell-specific things
}
}
The query fires off just fine, and in fact it returns the correct results. However, for some reason, this line,
my $names = $sth->{NAME} or die "Name failed: $DBI::errstr";
Fails. {NAME} never returns the arrayref I'd expect. If I take the die clause out, it runs fine (throwing the expected "using uninitialized values" warning wherever I'm using $names, of course).
Is there some obvious reason I'm missing that {NAME} wouldn't fire off, given that the query worked just fine?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我犯了一个严重的愚蠢错误。交换两条线,使其
修复。我想我必须在execute() 之后(或者更确切地说,在$sth 更改之前)直接获取{NAME}。我没想到 fetchall_arrayref 会擦除 {NAME}。
现在可以工作了!抱歉这个帖子。我会把这个留给后代,直到有人认为它不值得这个空间。 :-)
Big-time boneheaded mistake on my part. Switching two lines so that it's
Fixes it. I guess I have to grab for {NAME} directly after execute() (or rather, before $sth changes). I didn't expect fetchall_arrayref to wipe {NAME}.
Works now! Sorry for the post. I'll leave this up for posterity until someone decides it's not worth the space. :-)