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
In reply to Getting Perl Data Structures from DBI queries by jZed
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |