SQLite UPSERT / UPDATE OR INSERT

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

    You can also just add an ON CONFLICT REPLACE clause to your user_name unique constraint and then just INSERT away, leaving it to SQLite to figure out what to do in case of a conflict. See:https://sqlite.org/lang_conflict.html.

    Also note the sentence regarding delete triggers: When the REPLACE conflict resolution strategy deletes rows in order to satisfy a constraint, delete triggers fire if and only if recursive triggers are enabled.

提交回复
热议问题