On Wed, Nov 19, 2008 at 06:06, Rob Wilkerson <[EMAIL PROTECTED]> wrote:
> The truth is that 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 qw(:try);
>
> 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.
>
> Thanks.
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> http://learn.perl.org/
>
>
>

Check your connect call.  You may have PrintError => 1 in the options
(if you don't have PrintError => 0 then it is 1 by default).  Your
connect call should look something like this

my $dbh = DBI->connect(
    $dsn,
    $user,
    $pass,
    {
        AutoCommit => 1,
        PrintError => 0,
        RaiseError => 1,
        ChopBlanks => 1,
        FetchHashKeyName => 'NAME_lc'
    }
) or die "Could not open $dsn for $user: ", DBI->errstr;

I have never used the Error module, but it must be some form of source
filter that rewrites your code to use block evals.  Unless you are
getting something more from it than the try/catch syntax, you may want
to try using the block evals yourself:

eval {
       $sql = qq {
               INSERT INTO table (
                       field1,
                       field2,
                       field3,
                       field4,
                       field5
               )
               VALUES ( ?, ?, ?, ?, ? )
       };

       $sth = $mysql->prepare ( $sql );
       $sth->execute ( $value1, $value2, $value3, $value4, $value5 );
       $sth->finish();
}
if ($@) {
       $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();
};

See eval* and perlvar** for more information.


* perldoc -f eval or http://perldoc.perl.org/functions/eval.html
** perldoc perlvar or http://perldoc.perl.org/perlvar.html#$@

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to