SQLite - UPSERT *not* INSERT or REPLACE

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

    If you are generally doing updates I would ..

    1. Begin a transaction
    2. Do the update
    3. Check the rowcount
    4. If it is 0 do the insert
    5. Commit

    If you are generally doing inserts I would

    1. Begin a transaction
    2. Try an insert
    3. Check for primary key violation error
    4. if we got an error do the update
    5. Commit

    This way you avoid the select and you are transactionally sound on Sqlite.

提交回复
热议问题