Perl SQLite3: {NAME} not working?

Here is the code snippet from the 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 works 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 an array that I would expect. If I take the die die clause, it works fine (throwing the expected β€œusing uninitialized values” warning everywhere I use the $ names, of course).

Is there any obvious reason why I miss that {NAME} is not working, given that the request worked fine?

Thanks!

+4
source share
1 answer

A big quick mistake on my part. Switching two lines so that it

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

Fixed. I think I need to capture {NAME} immediately after execute () (or rather, before $ sth changes). I did not expect fetchall_arrayref to destroy {NAME}.

It works now! Sorry for posting. I will leave this for posterity until someone decides that it is not worth it. :-)

+5
source

All Articles