Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Having problems with DBI selectall_arrayref

by 1nickt (Canon)
on Dec 26, 2020 at 17:58 UTC ( [id://11125751]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11125751]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (None)
    As of 2024-04-25 03:58 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found