简体   繁体   English

替代 SQLite INSERT... ON CONFLICT... WHERE... DO UPDATE SET

[英]Alternative to SQLite INSERT ... ON CONFLICT ... WHERE ... DO UPDATE SET

I'm running an application that uses SQLite3 version 3.7.17 on Linux.我正在 Linux 上运行一个使用 SQLite3 版本 3.7.17 的应用程序。 It's erroring out on this statement:这个声明出错了:

INSERT INTO taxa (taxon_id, rank, parent_id) VALUES (?,?,?)
        ON CONFLICT (taxon_id) WHERE parent_id is NULL
        DO UPDATE SET parent_id=excluded.parent_id,rank=excluded.rank

But the same code runs on version 3.28.0.但是相同的代码在版本 3.28.0 上运行。 Is there another way of writing this statement so it can run on 3.7.17?是否有另一种编写此语句的方式,以便它可以在 3.7.17 上运行?

ON CONFLICT... or UPSERT was added to SQLite in version 3.24.0. ON CONFLICT...UPSERT已添加到版本 3.24.0 中的 SQLite。

In earlier versions you can get similar functionality with 2 separate statements.在早期版本中,您可以通过 2 个单独的语句获得类似的功能。

First try to update the table:首先尝试更新表:

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

If a row with the taxon_id =?如果一行带有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插入新行:

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

If a row with the taxon_id =?如果一行带有taxon_id =? exists nothing will happen (I assume that taxon_id is the PRIMARY KEY of the table or at least defined as UNIQUE ).存在什么都不会发生(我假设taxon_id是表的PRIMARY KEY或至少定义为UNIQUE )。
If it does not exist then the new row will be inserted.如果它不存在,则将插入新行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM