SQLite UPSERT / UPDATE OR INSERT

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

    This is a late answer. Starting from SQLIte 3.24.0, released on June 4, 2018, there is finally a support for UPSERT clause following PostgreSQL syntax.

    INSERT INTO players (user_name, age)
      VALUES('steven', 32) 
      ON CONFLICT(user_name) 
      DO UPDATE SET age=excluded.age;
    

    Note: For those having to use a version of SQLite earlier than 3.24.0, please reference this answer below (posted by me, @MarqueIV).

    However if you do have the option to upgrade, you are strongly encouraged to do so as unlike my solution, the one posted here achieves the desired behavior in a single statement. Plus you get all the other features, improvements and bug fixes that usually come with a more recent release.

    0 讨论(0)
提交回复
热议问题