SQLite - UPSERT *not* INSERT or REPLACE

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

    I think this may be what you are looking for: ON CONFLICT clause.

    If you define your table like this:

    CREATE TABLE table1( 
        id INTEGER PRIMARY KEY ON CONFLICT REPLACE, 
        field1 TEXT 
    ); 
    

    Now, if you do an INSERT with an id that already exists, SQLite automagically does UPDATE instead of INSERT.

    Hth...

提交回复
热议问题