Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

allowed

by Anonymous Monk
on Apr 05, 2001 at 02:03 UTC ( [id://69913]=note: print w/replies, xml ) Need Help??


in reply to Re: more and more dbi issues
in thread more and more dbi issues

am i allowed to have these statements in a loop or is this against dbase rules. does it compromise dbi standards to have 3 updates running 13 times in a loop??? is there protection against this. if this message doesn't make sense i've been coding 4 10hrs flat and am slowly shutting down. i hope u get the gist.

Replies are listed 'Best First'.
Re: allowed
by arturo (Vicar) on Apr 05, 2001 at 02:09 UTC

    According to House Bill 17289, in preparation, programming loops will no longer be able to nest beyond two levels deep. It looks like this sucker's going to become law, so you'd better hope that your code gets grandfathered in.

    In other news ... DBI won't prevent you from shooting yourself in the foot -- if you issue updates upon updates of the same row, it won't complain. Your DBMS might, on the other hand. It's up to you to ensure that your code's correct, here as nearly everywhere else.

    Philosophy can be made out of anything. Or less -- Jerry A. Fodor

      i took it out of the loop i removed the and statements and it works is there anything wrong with the and statements it works in sql manually i cant get it to work if there's even 1 and statement. i know someone who got it to work on the same system here with one and statement any ideas??

        I see nothing wrong with your SQL as written, though I will say this again:

        Use placeholders. They will prevent certain nasty kinds of errors from cropping up; one thing that might cause problems with your code as written is that IF there any quotes or other zany things in your data, that will result in the SQL string not being well-formed; if you use placeholders, these issues will be dealt with automatically. All this adds up to : use placeholders

        OK, with that out of the way ... check to see whether you've issued a commit to the database (this will have no effect if you're using a DB without commit and rollback, like vanilla MySQL), as sutch recommended. If the statements execute without error but nothing happens in the database, that's one place to look. Also, as thabenskta mentioned, you should always return the value of $dbh->errstr when a call to the database fails. So put those in, they'll help with debugging!

        Now, as to the method: whatdo is good for one-off things, but it forces you to prepare each statement each time through the loop. That's inefficient: prepare once, execute many times! So go with the normal prepare, bind, execute cycle:

        my $sth1 = $db->prepare(q{ UPDATE trees SET department = ? WHERE sport = ? AND round = ? }) or die "Can't prepare first statement: ". $dbh->errstr() . + "\n"; # and so with $sth2 and $sth3 # execution time for my $i (1..13) { # I'd suggest reworking this data structure too, but # I'll work with what you have # smaller sermon : initialize the variables that are # expected to change on each step through the loop # with my *** inside the loop *** # that way, you get a fresh copy of the variable each time # through without potentially having old values intrude my $team = $teams{"team$i"}; my $score = $scores{"score$i'}; $sth1->execute ($team, $sport, $i) or die "Execution of first update failed: ". $dbh->errstr() ." +\n"; $sth2->execute( #blah blah... }

        HTH.

        Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2024-04-18 07:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found