Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Using a better database fetch

by NetWallah (Abbot)
on Jan 16, 2013 at 16:56 UTC ( #1013614=note: print w/ replies, xml ) Need Help??

in reply to Using a better database fetch

I usually use a callback routine for convenience.
This reduces looping logic to one common place.
sub Fetch_w_callback{ # Main DBI retrieval mechanism---- my ($sql, $callback) = @_; my $state = $_[2] ||= {}; # ** This THIRD param is CALL-by-ref ** $debug{SQL} = $sql; (my $sth=$dbh->prepare($sql))->execute(); $state->{ROW} = 0; while (my $row = $sth->fetchrow_hashref()){ $debug{ROW} = $state->{ROW}++; last if $callback->($row,$state) < 0; # Return negative to quit } $sth->finish; } ## Sample call, later in the code .... Fetch_w_callback( "select client_name from xxx where cid='some-cid'", sub{ $row->{whatever} = $_[0]->{client_name}; # SET #Process other values );

             Most people believe that if it ain't broke, don't fix it.
        Engineers believe that if it ain't broke, it doesn't have enough features yet.

Comment on Re: Using a better database fetch
Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2016-05-27 09:42 GMT
Find Nodes?
    Voting Booth?