A recent chatterbox discussion made me aware that some otherwise cluefull people are not quite sure how to retrieve various perl data structures from a DBI query. Here's a simple example that illustrates the basics for Array of Arrays, Array of Hashes, and Hash of Hashes (all references). It should run with all DBDs.
#!perl -w use strict; use DBI; use Data::Dumper; my $dbh=DBI->connect('dbi:AnyData(RaiseError=1):'); my($create,$populate,$query,$keyfield)=split /\n/,join '',<DATA>; $dbh->do($_) for ($create,$populate); print 'AoA ', Dumper $dbh->selectall_arrayref($query); print 'AoH ', Dumper $dbh->selectall_arrayref($query,{Slice=>{}}); print 'HoH ', Dumper $dbh->selectall_hashref ($query,$keyfield); __DATA__ CREATE TABLE x (id INTEGER, phrase VARCHAR(20)) INSERT INTO x (id,phrase) VALUES (1,'foo') SELECT id,phrase FROM x id
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Getting Perl Data Structures from DBI queries
by helphand (Pilgrim) on Apr 22, 2004 at 02:53 UTC | |
by jZed (Prior) on Apr 22, 2004 at 03:53 UTC | |
by helphand (Pilgrim) on Apr 22, 2004 at 04:45 UTC | |
by jZed (Prior) on Apr 22, 2004 at 04:56 UTC |