SQLite - UPSERT *not* INSERT or REPLACE

后端 未结 18 2594
猫巷女王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:28

    If you don't mind doing this in two operations.

    Steps:

    1) Add new items with "INSERT OR IGNORE"

    2) Update existing items with "UPDATE"

    The input to both steps is the same collection of new or update-able items. Works fine with existing items that need no changes. They will be updated, but with the same data and therefore net result is no changes.

    Sure, slower, etc. Inefficient. Yep.

    Easy to write the sql and maintain and understand it? Definitely.

    It's a trade-off to consider. Works great for small upserts. Works great for those that don't mind sacrificing efficiency for code maintainability.

提交回复
热议问题