On Duplicate Key not working in SQLite

流过昼夜 提交于 2019-12-01 22:32:41

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/23622504/on-duplicate-key-not-working-in-sqlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!