Hi Tim, you asked about insert vs. update logic. . .

Assuming you have defined primary keys for the target table, you could
simply try
the insert and catch any failures.  If the $dbh->err code matches a
duplicate key
error, then do the update, otherwise print the error.  Here is a code
fragment:

my $dbh = DBI->connect (@connectParameters) 
    || die sprintf (qq(Cannot connect to %s: %s\n), $dbURL, DBI->errstr);
$dbh->{RaiseError} = 1;
$dbh->{PrintError} = 0;

# . . . do your prepare stuff here.

eval {
    $insert->bind_param ( 1, $cid, {TYPE=>SQL_INTEGER} );
    $insert->bind_param ( 2, $host, {TYPE=>SQL_VARCHAR} );
    $insert->execute();
};

if ($@) {
    if ( -803 == $dbh->err ) {
         eval {
            printf "We need to do an update.\n";
            $update->bind_param(1, $host, {TYPE=>SQL_VARCHAR});
            $update->bind_param(2, $cid, {TYPE=>SQL_INTEGER});
            $update->execute();
         };
         if ($@) {
             warn qq(Update failed: $@);
             $retval = 1;
         }
    }
    else {
        warn qq(Insert failed: $@);
        $retval = 1;
    }
}

Reply via email to