Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: How does DBI return an arrayref with key/value pairs?

by erix (Vicar)
on Jun 21, 2013 at 14:24 UTC ( #1040181=note: print w/ replies, xml ) Need Help??


in reply to How does DBI return an arrayref with key/value pairs?

#!/opt/perl-5.18/bin/perl use strict; use warnings; use DBI; main(); exit; sub main { my $dbh = DBI->connect('dbi:Pg:',undef,undef,{RaiseError=>1, Print +Error=>1, AutoCommit=>0,}) or die "oops - $!\n"; my $sql = " select * from ( values (1, 100, 1000) -- row 1 , (2, 200, 1500) -- row 2 ) as f(id, debet, credit) order by id; "; my $sth = $dbh->prepare( $sql ) or die "oops - prepare - $!\n"; my $rc = $sth->execute() or die "oops - execute - $!\n"; my %row; $sth->bind_columns( \( @row{ @{ $sth->{ NAME_lc} } } ) ); while ($sth->fetch) { print " id ", $row{ id } , "\n"; print " credit ", $row{ credit } , "\n"; print " debet ", $row{ debet } , "\n\n"; } $dbh->disconnect; } # note that fetch is an alias for fetchrow_arrayref

output:

$ perl arrayref.pl id 1 credit 1000 debet 100 id 2 credit 1500 debet 200


Comment on Re: How does DBI return an arrayref with key/value pairs?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2015-07-06 00:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (68 votes), past polls