SQLite UPSERT / UPDATE OR INSERT

后端 未结 7 718
时光说笑
时光说笑 2020-11-28 18:08

I need to perform UPSERT / INSERT OR UPDATE against a SQLite Database.

There is the command INSERT OR REPLACE which in many cases can be useful. But if you want to k

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-28 18:32

    The problem with all presented answers it complete lack of taking triggers (and probably other side effects) into account. Solution like

    INSERT OR IGNORE ...
    UPDATE ...
    

    leads to both triggers executed (for insert and then for update) when row does not exist.

    Proper solution is

    UPDATE OR IGNORE ...
    INSERT OR IGNORE ...
    

    in that case only one statement is executed (when row exists or not).

提交回复
热议问题