opensubscriber
   Find in this group all groups
 
Unknown more information…

p : perl-win32-database@listserv.ActiveState.com 11 October 2005 • 9:52PM -0400

RE: Help DBI Virgin
by MILLER David R.

REPLY TO AUTHOR
 
REPLY TO GROUP




If you want to read database results into a hash, I've grown to love a
little-documented feature of DBI. You can get all the results into an
arrayref pointing to a array of hashrefs. (This sounds more complicated
than it is.) It allows you to get the results fast, close the
connection, then iterate through your results. Example (untested):

# Assuming defined $dbh and prepared $sth
my $array_ref = $sth->fetchall_arrayref({}); # giving fetchall_arrayref
an empty hash reference makes it process results as hashes
$sth->finish;
$dbh->disconnect; # unless you have other work to do with this
connection
foreach my $array (@$array_ref) {
my %Ref = %$array;
#do stuff with the hash
}

This may require more code rewriting than you want, in which case use
fetchrow_hashref as someone else described. But file the technique away
for future reference.

-----------------------------------------
David R. Miller
Senior Software Developer
Medical Council of Canada
dmiller@mcc....
(613) 521-6012 x 2265
(613) 521-9722 (fax)
-----------------------------------------

Original message:
Message: 1
Date: Mon, 10 Oct 2005 15:12:17 +0100
From: "Ken Y. Brown" <ken.brown@dial...>
Subject: Help DBI Virgin
To: <perl-win32-database@list...>
Message-ID: <AFB904676C9A6E41A7A0C54915B4EE0C040C7F@DW10...>
Content-Type: text/plain; charset="us-ascii"

I've been using win32::odbc for a while now and have started porting to
DBI but I'm confused

Is there an easy way to do the equiv of

$Sql = "SELECT * FROM TABLE";
<do the exec stuff......>
While ($handle->FetchRow()) {
%Ref = $handle->DataHash();
<do stuff with the hash i.e. if ($Ref{'COL_NAME'} eq "SOMETHING") {<do
something...>}>
}

I'm guessing there is but don't know how

Reason

All the rest of the code uses hashes and I don't want to have to rewrite
the whole system just to get DBI working

_______________________________________________
Perl-Win32-Database mailing list
Perl-Win32-Database@list...
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Bookmark with:

Delicious   Digg   reddit   Facebook   StumbleUpon

opensubscriber is not affiliated with the authors of this message nor responsible for its content.