R
Rob Wilkerson
Disclaimer: I know enough Perl to get by when I absolutely have
to use it, so maybe this is the correct behavior, but having never
seen it in any other language, I thought I'd post the question to the
group and maybe learn something.
I have a scenario where I need to try to insert a record into a MySQL
database and, if the insert fails due to, well, anything, then perform
an update instead. To do this, I'm using the try/catch construct of
the Error module:
use Error qwtry);
try {
$sql = qq {
INSERT INTO table (
field1,
field2,
field3,
field4,
field5
)
VALUES ( ?, ?, ?, ?, ? )
};
$sth = $mysql->prepare ( $sql );
$sth->execute ( $value1, $value2, $value3, $value4, $value5 );
$sth->finish();
}
catch Error with {
$sql = qq {
UPDATE table
SET field1 = ?,
field2 = ?,
field3 = ?
WHERE field4 = ?
AND field5 = ?
};
$sth = $mysql->prepare ( $sql );
$sth->execute ( $value1, $value2, $value3, $value4, $value5 );
$sth->finish();
};
The code seems to be doing exactly what I expect. That is, dropping
into the catch block and performing the update where a record exists,
but the errors being caught are not being suppressed. I still get a
lot of:
DBD::mysql::st execute failed: Duplicate entry '121993-14196' for key
1 at ./get_metrics.pl line 247.
It's not the end of the world, but I'd prefer to suppress the message
if there's a way to do that. Is this expected behavior? It certainly
caught me by surprise and I spent a while trying to debug until I
realized that if I looked past the messages, the work was getting
done.
I did try using the eval{} if ($@){} combo, but got the same result.
Thanks.
to use it, so maybe this is the correct behavior, but having never
seen it in any other language, I thought I'd post the question to the
group and maybe learn something.
I have a scenario where I need to try to insert a record into a MySQL
database and, if the insert fails due to, well, anything, then perform
an update instead. To do this, I'm using the try/catch construct of
the Error module:
use Error qwtry);
try {
$sql = qq {
INSERT INTO table (
field1,
field2,
field3,
field4,
field5
)
VALUES ( ?, ?, ?, ?, ? )
};
$sth = $mysql->prepare ( $sql );
$sth->execute ( $value1, $value2, $value3, $value4, $value5 );
$sth->finish();
}
catch Error with {
$sql = qq {
UPDATE table
SET field1 = ?,
field2 = ?,
field3 = ?
WHERE field4 = ?
AND field5 = ?
};
$sth = $mysql->prepare ( $sql );
$sth->execute ( $value1, $value2, $value3, $value4, $value5 );
$sth->finish();
};
The code seems to be doing exactly what I expect. That is, dropping
into the catch block and performing the update where a record exists,
but the errors being caught are not being suppressed. I still get a
lot of:
DBD::mysql::st execute failed: Duplicate entry '121993-14196' for key
1 at ./get_metrics.pl line 247.
It's not the end of the world, but I'd prefer to suppress the message
if there's a way to do that. Is this expected behavior? It certainly
caught me by surprise and I spent a while trying to debug until I
realized that if I looked past the messages, the work was getting
done.
I did try using the eval{} if ($@){} combo, but got the same result.
Thanks.