SQLite - UPSERT *not* INSERT or REPLACE

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

    This answer has be updated and so the comments below no longer apply.

    2018-05-18 STOP PRESS.

    UPSERT support in SQLite! UPSERT syntax was added to SQLite with version 3.24.0 (pending) !

    UPSERT is a special syntax addition to INSERT that causes the INSERT to behave as an UPDATE or a no-op if the INSERT would violate a uniqueness constraint. UPSERT is not standard SQL. UPSERT in SQLite follows the syntax established by PostgreSQL.

    alternatively:

    Another completely different way of doing this is: In my application I set my in memory rowID to be long.MaxValue when I create the row in memory. (MaxValue will never be used as an ID you will won't live long enough.... Then if rowID is not that value then it must already be in the database so needs an UPDATE if it is MaxValue then it needs an insert. This is only useful if you can track the rowIDs in your app.

提交回复
热议问题