5

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.

Neeku
  • 3,646
  • 8
  • 33
  • 43
Alireza
  • 209
  • 2
  • 3
  • 10

2 Answers2

13

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

bansi
  • 55,591
  • 6
  • 41
  • 52
2

Since SQLite version 3.24.0 (released 2018-06-04) there is ON CONFLICT support modeled after PostgreSQL. See documentation for more details.

Mitar
  • 6,756
  • 5
  • 54
  • 86