http://qs321.pair.com?node_id=11125751


in reply to Having problems with DBI selectall_arrayref

Hi,

"What I am trying to get back is an array of hash references, that's al, to my multiple row query"

You may just have a dereferencing problem, but anyway I think you are reaching for Slice with an empty value?

use strict; use warnings; use feature 'say'; use DBD::SQLite; use Data::Dumper; # set up and populate an in-memory DB my $dbh = DBI->connect("dbi:SQLite:dbname=:memory:", undef,undef,{ RaiseError => 1 }); $dbh->do(q{ create table data( id integer primary key, last_name text, first_name text, role text) }); my $insert_sth = $dbh->prepare(q{ insert into data values (?,?,?,?); }); while ( my $record = <DATA> ) { chomp($record); my @values = split(' ', $record); $insert_sth->execute(@values); } # Select everything (!) my $ref = $dbh->selectall_arrayref(q{ select * from data }, { Slice => {} }); say Dumper $ref; __END__ 1 Flintstone Fred Father 2 Flintstone Wilma Mother 3 Flinstone Pebbles Child 4 Flintstone Dino Pet 5 Rubble Barney Father 6 Rubble Betty Mother 7 Rubble Bam-Bam Child

Output:

$VAR1 = [ { 'first_name' => 'Fred', 'last_name' => 'Flintstone', 'role' => 'Father', 'id' => 1 }, { 'first_name' => 'Wilma', 'last_name' => 'Flintstone', 'id' => 2, 'role' => 'Mother' }, { 'id' => 3, 'role' => 'Child', 'last_name' => 'Flinstone', 'first_name' => 'Pebbles' }, { 'first_name' => 'Dino', 'last_name' => 'Flintstone', 'role' => 'Pet', 'id' => 4 }, { 'last_name' => 'Rubble', 'role' => 'Father', 'id' => 5, 'first_name' => 'Barney' }, { 'first_name' => 'Betty', 'last_name' => 'Rubble', 'id' => 6, 'role' => 'Mother' }, { 'role' => 'Child', 'id' => 7, 'last_name' => 'Rubble', 'first_name' => 'Bam-Bam' } ];

To print how your post has it:

for my $row ( @$ref ) { for my $colname ( keys %$row ) { say $colname; say $row->{ $colname }; } }

Hope this helps!


The way forward always starts with a minimal test.