Comments below.
--
Mac :})
** I normally forward private database questions to the DBI mail lists. **
Give a hobbit a fish and he'll eat fish for a day.
Give a hobbit a ring and he'll eat fish for an age.
----- Original Message -----
From: "Kamp, Rolf F, ALSVC" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, March 06, 2001 2:15 PM
Subject: RaiseError on and off
> I see the example in the Perl DBI book dealing with setting
> RaiseError OFF when connecting, then turning it ON in a block. I am trying
> to do the opposite, with no success.
> I connect to the database with RaiseError ON:
> my $dbh = DBI->connect(undef, 'login', 'passwd', {RaiseError=>1}) or die
> ("Cannot connect to the database " . $DBI::errstr);
> Then I have a bunch of DBI statements that I would like the program
> to croak on if an error occurs:
>
> $usg_insert = $dbh->prepare(q{
> insert into usage(sid,interval,usage)
> values(?, to_date(?, 'MM-DD-YYYY-HH24:MI:SS'), ?)
> });
> $usg_update = $dbh->prepare(q{
> update usage set usage = usage + ?
> where sid = ? and interval = to_date(?, 'MM-DD-YYYY-HH24:MI:SS')
> });
>
> Then I have an insert that I would like to catch the error from,
> test if the insert failed for a violation of a unique constraint
(duplicate
> row), then try an update:
>
> if ($usg_select->fetch()) {
> $dbh->{RaiseError} = 0; # shut error catching OFF (can't get this
> right)
> if (!$usg_insert->execute($sid, $startTimeForOracle, $tbytes)) {
> $dbh->{RaiseError} = 1; # turn error catching back on
> print "INSERT OF USAGE FAILED $DBI::err $DBI::errstr\n";
> if ($DBI::err == 1) {
> if(!$usg_update->execute($tbytes, $sid, $startTimeForOracle)) {
> print "usage update failed $DBI::err $DBI::errstr\n";
You will never execute this print() since you've already turned RaiseError
back on.
> }
> }
> }
> $dbh->{RaiseError} = 1;
> }
> I cannot seem to get DBI to allow me to handle the failed insert.
> Any ideas or suggestions are greatly appreciated.
This looks reasonable to me. What output do you receive? Just to be sure,
make sure STDERR and STDOUT are unbuffered to be sure you aren't losing
output from your print statements.