mysql - ON DUPLICATE KEY UPDATE won't work -


i'm trying hours make simple mysql command , won't work. have database package ids apps stored, 'com.android.package'. stored so:

| id | packageid           | packagedesc       | | 1  | com.android.package | package | | 2  | com.android.test2pa | package 2 |  , on... 

now want insert new entry, if 'com.android.package' don't exist , if exists, want nothing...

i've tried following:

 insert packages (packageid) values ('com.android.package') on duplicate key update packageid=values(packageid) 

but still creates new entries, that:

| id | packageid           | packagedesc       | | 3  | com.android.package |                   | | 4  | com.android.package |                   | | 5  | com.android.package |                   | 

i don't know where's error.

a proposed packages table schema on duplicate key update work expected following:

create table packages (    `id` int not null primary key auto_increment,     `packageid` varchar(30) not null,    `packagedesc` varchar (100),    unique(`packageid`) ); 

after statement executed:

insert packages (packageid, packagedesc) values  ('com.android.package', 'this package')  on duplicate key update packageid=values(packageid); 

packages contains following row:

# id, packageid,            packagedesc '1', 'com.android.package', 'this package' 

if execute following statement:

insert packages (packageid, packagedesc) values  ('com.android.package', 'this package2')  on duplicate key update packageid=values(packageid), packagedesc=values(packagedesc); 

packages contains:

# id, packageid,            packagedesc '1', 'com.android.package', 'this package2' 

this means update performed latter insert into.

demo here


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -