开发者

Perl's SQLite3: {NAME} not working?

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!


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). 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. :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜