user462879
user462879

Reputation: 187

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!

Upvotes: 4

Views: 175

Answers (1)

user462879
user462879

Reputation: 187

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

Upvotes: 5

Related Questions