MySQL Lists are EOL. Please join:

List:General Discussion« Previous MessageNext Message »
From:Harald Fuchs Date:July 27 2004 9:30am
Subject:Re: INSERT if record NOT EXISTS
View as plain text  
In article <1090860457.3176.76.camel@stripped>,
Adaikalavan Ramasamy <ramasamy@stripped> writes:

> This seems more like the solution I want. I am using perl-DBI and when
> there is an error (i.e. duplicate insert), the rest of the scrip it not
> executed. But this is gives me the following error. What am I doing
> wrong ?

mysql> desc tb;
> +-----------+-------------+------+-----+---------+----------------+
> | Field     | Type        | Null | Key | Default | Extra          |
> +-----------+-------------+------+-----+---------+----------------+
> | myID      | int(11)     |      | PRI | NULL    | auto_increment |
> | firstname | varchar(10) | YES  | MUL | NULL    |                |
> | lastname  | varchar(10) | YES  |     | NULL    |                |
> +-----------+-------------+------+-----+---------+----------------+
> 3 rows in set (0.00 sec)

mysql> INSERT INTO tb (firstname, lastname) VALUES ('Jack', 'Doe') ON
> DUPLICATE KEY UPDATE lastname = lastname;
> ERROR 1064: You have an error in your SQL syntax near 'ON DUPLICATE KEY
> UPDATE lastname = lastname' at line 1

The "ON DUPLICATE KEY..." clause is new in MySQL 4.1.0.

> Alternatively, I am looking for 'try' equivalent in perl, so that if the
> insert is duplicate, the rest of the script is still run. Thank you.

Most people use DBI and DBD::mysql to access a MySQL server from Perl.
Then you don't need something like "try".  Just disable
$dbh->{RaiseError}, execute the INSERT, and then check $dbh->err.

Thread
INSERT if record NOT EXISTSAdaikalavan Ramasamy26 Jul
  • Re: INSERT if record NOT EXISTSAlec.Cawley26 Jul
  • Re: INSERT if record NOT EXISTSgerald_clark26 Jul
  • Re: INSERT if record NOT EXISTSMichael Dykman26 Jul
    • Re: INSERT if record NOT EXISTSAdaikalavan Ramasamy26 Jul
      • Re: INSERT if record NOT EXISTSMarc Slemko26 Jul
  • Re: INSERT if record NOT EXISTSHarald Fuchs27 Jul
Re: INSERT if record NOT EXISTSAdaikalavan Ramasamy26 Jul
  • Re: INSERT if record NOT EXISTSAlec.Cawley26 Jul
  • Re: INSERT if record NOT EXISTSKeith Ivey26 Jul
    • Re: INSERT if record NOT EXISTSAdaikalavan Ramasamy26 Jul