in reply to DBI hashref does not return data in order of query

hashes are used for accessing data by (string) keys and they are unsorted. use fetchrow_arrayref instead if you want sorted rows (indexed by number)

  • Comment on Re: DBI hashref does not return data in order of query

Replies are listed 'Best First'.
Re^2: DBI hashref does not return data in order of query
by hallikpapa (Scribe) on Oct 23, 2007 at 01:18 UTC
    Thanks for the info. That seems to have worked but all my XML element tags now say anon, instead of being the column header of the query results.
    my %xml_hash = ( dataset => [] ); while ( my $row = $sth->fetchrow_arrayref ) { push @{ $xml_hash{cdr} }, $row; #print Dumper($row); }
    Any pointers on how to do that? Thanks again!

      A couple things I noticed, 1) you are assigning the array reference to the same hash key each iteration through the loop (is this what you want?), 2) pushing the array ref into the hash gives you an anonymous array within an anonymous array, which I don't think is what you want. It might make more sense to assign the row to the key (provided you are using different keys through the loop).

      If you just want an array of array references (unless there's a field in the returned row that is unique enough to use as a hash key) you can try:

      my @allrows = (); while (my $row = $sth->fetchrow_arrayref) { push @allrows, $row; }
        The problem is since they can choose their own data, it's entire possible to not have any unique columns on their own. Trying your example I <anon> element tags again. I am using XML::Simple to create the xml.
        my @xml_array = (); while ( my $row = $sth->fetchrow_arrayref ) { push @xml_array, $row; } my $cgi = CGI->new; print $cgi->header('text/xml'); print Dumper( \@xml_array ); my $testxml = XMLout( \@xml_array, NoAttr => 1, RootName => 'datas +et', ); print $testxml;
        In another section, I use XML::Generator, like this. Maybe somehow I can loop through so the right amount of columns are created in this hash, and it's generated on it's own. UNLESS it's possible to get the column names in the element tags using the above array solution, I just can't see it. :(
        $xml->cdr( $xml->Date($date_time), $xml->IngressTG($orig_gw), $xml->EgressTG($term_gw), $xml->Dialed($call_dtmf), $xml->DUR($duration), $xml->PDD($hold_time), $xml->ISDN($isdn), $xml->CallType($call_type), $xml->Link("URL") );