SQLite - UPSERT *not* INSERT or REPLACE

后端 未结 18 2614
猫巷女王i
猫巷女王i 2020-11-21 23:53

http://en.wikipedia.org/wiki/Upsert

Insert Update stored proc on SQL Server

Is there some clever way to do this in SQLite that I have not thought of?

18条回答
  •  失恋的感觉
    2020-11-22 00:30

    INSERT OR REPLACE is NOT equivalent to "UPSERT".

    Say I have the table Employee with the fields id, name, and role:

    INSERT OR REPLACE INTO Employee ("id", "name", "role") VALUES (1, "John Foo", "CEO")
    INSERT OR REPLACE INTO Employee ("id", "role") VALUES (1, "code monkey")
    

    Boom, you've lost the name of the employee number 1. SQLite has replaced it with a default value.

    The expected output of an UPSERT would be to change the role and to keep the name.

提交回复
热议问题