【发布时间】:2013-08-29 08:15:13
【问题描述】:
我不知道如何查询 SQLite。 需要: 1)替换记录(主键),如果条件(新旧字段条目的比较) 2) 如果主键上的数据库中不存在这样的条目,则插入一个条目。
重要的是,它必须工作得非常快! 我无法提出有效的调查。
编辑。
MyInsertRequest - 所需的表达式。
脚本:
CREATE TABLE testtable (a INT PRIMARY KEY, b INT, c INT)
INSERT INTO testtable VALUES (1, 2, 3)
select * from testtable
1|2|3
-- Adds an entry, because the primary key is not
++ MyInsertRequest VALUES (2, 2, 3) {if c>4 then replace}
select * from testtable
1|2|3
2|2|3
-- Adds
++ MyInsertRequest VALUES (3, 8, 3) {if c>4 then replace}
select * from testtable
1|2|3
2|2|3
3|8|3
-- Does nothing, because such a record (from primary key field 'a')
-- is in the database and none c>4
++ MyInsertRequest VALUES (1, 2, 3) {if c>4 then replace}
select * from testtable
1|2|3
2|2|3
3|8|3
-- Does nothing
++ MyInsertRequest VALUES (3, 34, 3) {if c>4 then replace}
select * from testtable
1|2|3
2|2|3
3|8|3
-- replace, because such a record (from primary key field 'a')
-- is in the database and c>2
++ MyInsertRequest VALUES (3, 34, 1) {if c>2 then replace}
select * from testtable
1|2|3
2|2|3
3|34|1
【问题讨论】:
-
如果最后我想添加
VALUES (1, 2, 1)怎么办?我看不懂标准!是不是:添加新的a,当a匹配并且new.c小于old.c时替换,否则什么都不做?
标签: sqlite insert replace conditional-statements