Alternative to SQLite INSERT … ON CONFLICT … WHERE … DO UPDATE SET

前端 未结 1 839
清酒与你
清酒与你 2021-01-25 12:28

I\'m running an application that uses SQLite3 version 3.7.17 on Linux. It\'s erroring out on this statement:

INSERT INTO taxa (taxon_id, rank, parent_id) VALUES          


        
相关标签:
1条回答
  • 2021-01-25 13:07

    ON CONFLICT... or UPSERT was added to SQLite in version 3.24.0.

    In earlier versions you can get similar functionality with 2 separate statements.

    First try to update the table:

    UPDATE taxa 
    SET rank = ?, parent_id = ?
    WHERE taxon_id = ?;
    

    If a row with the taxon_id = ? exists it will be updated.
    If it does not exist nothing will happen.

    Then try to insert the new row with INSERT OR IGNORE:

    INSERT OR IGNORE INTO taxa (taxon_id, rank, parent_id) VALUES (?, ?, ?);
    

    If a row with the taxon_id = ? exists nothing will happen (I assume that taxon_id is the PRIMARY KEY of the table or at least defined as UNIQUE).
    If it does not exist then the new row will be inserted.

    0 讨论(0)
提交回复
热议问题