简体   繁体   English

Perl的SQLite3:{NAME}不工作?

[英]Perl's SQLite3: {NAME} not working?

Here's a snippet of code from an sqlite database application I'm working on: 这是我正在处理的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
    }
}

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. {NAME}永远不会返回我期望的arrayref。 If I take the die clause out, it runs fine (throwing the expected "using uninitialized values" warning wherever I'm using $names, of course). 如果我把die子句删掉,它运行正常(当然,无论我在哪里使用$ name,都会抛出预期的“使用未初始化的值”警告)。

Is there some obvious reason I'm missing that {NAME} wouldn't fire off, given that the query worked just fine? 是否有一些明显的原因我错过了{NAME}不会触发,因为查询工作得很好?

Thanks! 谢谢!

Big-time boneheaded mistake on my part. 我身上出现了很大的愚蠢错误。 Switching two lines so that it's 切换两条线以便它

my $names ...
my $result ...

Fixes it. 修复它。 I guess I have to grab for {NAME} directly after execute() (or rather, before $sth changes). 我想我必须在执行()之后直接抓取{NAME}(或者更确切地说,在$ sth更改之前)。 I didn't expect fetchall_arrayref to wipe {NAME}. 我没想到fetchall_arrayref会擦除{NAME}。

Works now! 现在就行! Sorry for the post. 对不起该帖子。 I'll leave this up for posterity until someone decides it's not worth the space. 我会把它留给子孙后代,直到有人认定它不值得这个空间。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM