Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Result set into array

by novicepl (Initiate)
on Nov 19, 2006 at 00:35 UTC ( [id://584904]=perlquestion: print w/replies, xml ) Need Help??

novicepl has asked for the wisdom of the Perl Monks concerning the following question:

How can I get result set into an array.
Query is : Select id, col2, col3, col4 From TableA Array should be as below: (1, 2 are id from TableA) %arry = ( 1 => {col2=>"abc", col3=>"def", col4=>"ghi"}, 2 => {col2=>"lmn", col3=>"opq", col4=>"rst"}, )

Replies are listed 'Best First'.
Re: Result set into array
by imp (Priest) on Nov 19, 2006 at 02:41 UTC
    Your question specified that you would like the results in an array, but the example output you provided is a hash, and you used selectall_hashref in your earlier reply.

    If you would like to fetch all of the rows and have them in a hash then you need to specify which column should be used, as in this example:

    my $hoh = $dbh->selectall_hashref($sql, $index_column);
    In this example the structure would be as follows:
    $hoh = { 1 => {id => 1, col2=>"abc", col3=>"def", col4=>"ghi"}, 2 => {id => 2, col2=>"lmn", col3=>"opq", col4=>"rst"}, }
    Keep in mind that as it is returning a hashref the order of the rows will not be preserved. To preserve the order you would use selectall_arrayref as follows:
    my $aoa = $dbh->selectall_arrayrefref($sql);
    In this example the structure would be as follows:
    $aoa = [ [1,"abc", "def", "ghi"], [2,"lmn", "opq", "rst"], ]
    If you would like to preserve both the order and the column names, then you can use the Slice option for selectall_arrayref as follows:
    my $aoh = $dbh->selectall_arrayref($sql, {Slice => {}});
    In this example the structure would be as follows:
    $aoh = [ {id => 1, col2=>"abc", col3=>"def", col4=>"ghi"}, {id => 2, col2=>"lmn", col3=>"opq", col4=>"rst"}, ]
Re: Result set into array
by jZed (Prior) on Nov 19, 2006 at 00:46 UTC
    Although you don't say, I assume you are using DBI. Try something like
    my $hash = $dbh->selectall_hashref( "Select id, col2, col3, col4 From TableA". 'id' );
      yes, I am using DBI. getting error msg "Can't locate object method "selectall_hashref" via package "DBI::st" at ./Test1.pl line 166. "
      $sth = $dbconn->prepare($sql_statement); $sth->execute() or die "Can not execute: " . $sth->errstr; $sth->{RaiseError} = 1; my $hash = $sth->selectall_hashref($sql_statement); #this is line +166 $sth->finish; $dbconn->commit(); $dbconn->disconnect();
        Guessing from reading the docs (!) but it looks like selectall_hashref() is executed using the DB handle, so your code might better be:
        my $hash = $dbconn->selectall_hashref($sql_statement);
        You may have been led astray by the talk of preparing a statement ahead of time, but even then you still use the DB handle.
        my $sth = $dbconn->prepare($sql_statement); . . . . . . . my $hash = $dbconn->selectall_hashref($sth);
        As for setting the valuable RaiseError flag, it looks like you'll have to do that on the DB handle when using selectall_hashref, perhaps something like:
        { local $dbconn->{RaiseError} = 1; my $hash = $dbconn->selectall_hashref($sql_statement); . . . . . . . }
        Go back and read the DBI docs whenever something goes 'wrong' - sometimes it takes me a couple "read again"s before it sinks in to my flat file head.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-20 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found