Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On Duplicate Key not working in SQLite

In my table, id is the primary key, but this code not working in sqlite3:

insert into text (id,text) VALUES(150574,'Hello') ON DUPLICATE KEY UPDATE 'text' = 'good'

Please help me.

like image 861
Alireza Avatar asked May 13 '14 03:05

Alireza


1 Answers

INSERT .... ON DUPLICATE don't exist in SqLite. But you can use INSERT OR REPLACE to achieve the effect like the following.

INSERT 
    OR REPLACE
INTO
    text (id, text)  
VALUES
    (150574,
        (SELECT
           CASE 
              WHEN exists(SELECT 1  FROM text WHERE id=150574)
              THEN 'good' 
              ELSE 'Hello' 
           END
         )
    )

Ref: http://www.sqlite.org/lang_insert.html

like image 190
bansi Avatar answered Sep 25 '22 14:09

bansi