SQLite UPSERT - 重复键更新 [英] SQLite UPSERT - ON DUPLICATE KEY UPDATE
本文介绍了SQLite UPSERT - 重复键更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
MySQL有类似这样的东西:
INSERT INTO访问(ip,hits)
VALUES 0.1',1)
on DUPLICATE KEY UPDATE hits = hits + 1;
据我所知,SQLite中不存在此功能,是如果有任何方式来归档相同的效果,而不必执行两个查询。此外,如果这不可能,您喜欢什么:
- SELECT +(INSERT或UPDATE)
- UPDATE(+ INSERT if UPDATE failed div class =h2_lin>解决方案
INSERT或IGNORE INTO访问VALUES($ ip,0);
UPDATE visits SET hits = hits + 1 WHERE ip LIKE $ ip;
这需要ip列有一个UNIQUE(或PRIMARY KEY)约束。 >
编辑:另一个伟大的解决方案: http ://stackoverflow.com/a/4330694/89771 。
MySQL has something like this:
INSERT INTO visits (ip, hits)
VALUES ('127.0.0.1', 1)
ON DUPLICATE KEY UPDATE hits = hits + 1;
As far as I'm know this feature doesn't exist in SQLite, what I want to know is if there is any way to archive the same effect without having to execute two queries. Also, if this is not possible, what do you prefer:
- SELECT + (INSERT or UPDATE) or
- UPDATE (+ INSERT if UPDATE fails)
解决方案
INSERT OR IGNORE INTO visits VALUES ($ip, 0);
UPDATE visits SET hits = hits + 1 WHERE ip LIKE $ip;
This requires the "ip" column to have a UNIQUE (or PRIMARY KEY) constraint.
EDIT: Another great solution: http://stackoverflow.com/a/4330694/89771.
这篇关于SQLite UPSERT - 重复键更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文