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.

In reply to Re: Having problems with DBI selectall_arrayref by 1nickt
in thread Having problems with DBI selectall_arrayref by SergioQ

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.