Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

multiple queries:

by anjaana78 (Novice)
on May 17, 2001 at 19:54 UTC ( #81283=perlquestion: print w/replies, xml ) Need Help??
anjaana78 has asked for the wisdom of the Perl Monks concerning the following question:

i have to execute multiple queries one afer the other. problem i am facing is if i am getting an error in one query my program is not executing after that query its getting killed. the code which i am using is as follows:
while( $row = $select_handle->fetchrow_hashref)#getting row from some +other query { $handle = $syb_handle->prepare($query) or die "Unable to prepare sat +ement"; $handle->execute or die "Unable to execute query"; $allrows = $handle->fetchall_arrayref(); my($i, $j); for $i ( 0 .. $#{$allrows} ) { for $j ( 0 .. $#{$allrows->[$i]} ) { print $allrows->[$i][$j]; }#ending $j print "\n"; }#ending the $i }#ending while
i want that all the rows be executed and even if some of the queries get errors. thank you. - faaiz

Replies are listed 'Best First'.
Re: multiple queries:
by tinman (Curate) on May 17, 2001 at 21:25 UTC

    I'm not sure what the error from your query is... so, there are several assumptions that I will make..

    Assuming there is a dependency between the first query and the second (ie: the second query uses part of the result from the first query), just perform your validation before calling the second query..

    Some things that I see in your code, calling prepare repeatedly is not efficient, consider using placeholders in your query code, read this node if you're not sure about placeholders...also, it does not appear that you are using the -w switch and running your code under strict.. consider doing this, it can save you lots of debugging effort, and its not too hard to incorporate either...

    So, your code can look like,

    my $handle = $syb->prepare($query); while(my @row = $select->fetchrow_array()) { # assuming that $row[0] is what you want to use in the # next query if($row[0] == 'ok') # it passes validation, yay { $handle->execute($row[0]); # fetch all the rows here if you want } else { # this is how you trap errors, leave this blank if you wi +sh } }

Re: multiple queries:
by Anonymous Monk on May 17, 2001 at 21:32 UTC
    Goto this Perlguy Inc website by Brent Michalski (I guess, as there is no obvious info about the page's author):

    In the subroutine Do_SQL, notice how the author made use of EVAL.
    You may use subroutines similar to Create_DB_Connection and Do_SQL in your codes, or you can use EVAL for your own purpose.

Re: multiple queries:
by Anonymous Monk on May 17, 2001 at 21:44 UTC
    Oh in the Do_SQL example, you better omit the line EXIT if you still want the perl script continue to run.

    Please also take a look at for function die() and eval().

    Here's an example from perlfunc page of die():
    eval { ... }; die unless $@ =~ /Expected exception/;
Re: multiple queries:
by busunsl (Vicar) on May 18, 2001 at 09:41 UTC
    One thing I have come across is that you have to finish a statement before executing a new one.
    Insert a $handle->finish(); before the }#ending while

    It would help us, if you would provide the error message(s), perhaps one query has the wrong syntax.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://81283]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2018-02-23 20:56 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (310 votes). Check out past polls.