DooDah has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks, I don't know if I'm missing the obvious here, but I'm trying to use eval to catch errors in writing to a database. Here is a piece of code I'm using to test the idea.
$dbh->{AutoCommit} = 0; $dbh->{RaiseError} = 1; eval { $sql = "UPDATE stockhistory SET invoiced = 1 WHERE id = 737"; $sth = $dbh->prepare($sql) or die "Cannot prepare: " . $dbh->err +str(); $sth->execute(); }; $dbh->commit (); if ($@) { print "Transaction aborted because $@"; $dbh->rollback; }
If I change the name of the database $@ shows an error (as you would expect) as does changing the field name 'invoiced' or 'id'.
However, if I change the id number to a number that does not exist in the db (say 1737), no error is shown even though it cannot have written that id number. Nothing changes in the db but the script carries on as if it has written successfully.
Can anyone tell me where I'm going wrong?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Eval not working
by Utilitarian (Vicar) on Sep 03, 2010 at 11:03 UTC | |
by DooDah (Novice) on Sep 03, 2010 at 11:11 UTC | |
by ikegami (Patriarch) on Sep 03, 2010 at 15:52 UTC | |
by DooDah (Novice) on Sep 03, 2010 at 20:07 UTC | |
by TomDLux (Vicar) on Sep 03, 2010 at 14:26 UTC | |
|
Re: Eval not working
by Anonymous Monk on Sep 09, 2010 at 21:52 UTC |