Home: Perl Programming Help: DBI:
SELECT Finding if result returned is 0



sarthakganguly
Novice

Jul 25, 2009, 11:34 PM


Views: 7694
SELECT Finding if result returned is 0

Hi,
I have the following script for fetching some data from mySql DB.

my $query_select_groups="SELECT GROUPS.group_id,GROUPS.article_id FROM GROUPS WHERE group_id = (SELECT DISTINCT GROUPS.group_id FROM GROUPS LIMIT 1) AND flag=0";

my $query_select_groups_handle = $connection->prepare($query_select_groups);

$query_select_groups_handle->execute() or die "Error in fetching data";

query_select_groups_handle->bind_columns(\$group_id,\$article_id);

while($query_select_groups_handle->fetch()){
print "group id $group_id \n ";
print "article id $article_id \n";
push(@articles,$article_id);
}

If the query returns 0 rows I want the program to sleep for 30 secs and try again.

q1)How to find if the query is returning 0 or not? Is this going to help ....?
if($query_select_groups_handle->{NUM_OF_FIELDS} eq 0){
goToSleep();
}

q2)If some error occurs I dont want the program to die..I want it to log and go to sleep. For dat instead of

$query_select_groups_handle->execute() or die "Error in fetching data";

what can I use?
Please suggest me some solutions...
Thanks in Advance

Thanking you
regards
Sarthak


FishMonger
Veteran / Moderator

Jul 26, 2009, 8:45 AM


Views: 7689
Re: [sarthakganguly] SELECT Finding if result returned is 0

A1) http://search.cpan.org/~timb/DBI-1.609/DBI.pm#rows

A2) Write a subroutine that does what you want and call that sub instead of die.


sarthakganguly
Novice

Jul 26, 2009, 8:50 AM


Views: 7687
Re: [FishMonger] SELECT Finding if result returned is 0

Hi,
Thanks a lot for ur reply.
A1)I have gone through the documentation on cpan but couldn't understand how to do it.
A2)Thanks a lot for it.

Thanking you
regards
Sarthak


FishMonger
Veteran / Moderator

Jul 26, 2009, 9:12 AM


Views: 7685
Re: [sarthakganguly] SELECT Finding if result returned is 0


Code
while($query_select_groups_handle->fetch()){ 
print "group id $group_id \n ";
print "article id $article_id \n";
push(@articles,$article_id);
}

if( $query_select_groups_handle->rows < 1 ) {
goToSleep();
}



sarthakganguly
Novice

Jul 26, 2009, 9:20 AM


Views: 7683
Re: [FishMonger] SELECT Finding if result returned is 0

Thanks a lot for the quick reply.