SQLite UPSERT / UPDATE OR INSERT

后端 未结 7 733
时光说笑
时光说笑 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:50

    To have a pure UPSERT with no holes (for programmers) that don't relay on unique and other keys:

    UPDATE players SET user_name="gil", age=32 WHERE user_name='george'; 
    SELECT changes();
    

    SELECT changes() will return the number of updates done in the last inquire. Then check if return value from changes() is 0, if so execute:

    INSERT INTO players (user_name, age) VALUES ('gil', 32); 
    

提交回复
热议问题