SQLite 中的条件查询

Conditional query in SQLite

在 sqlite 中做什么在大多数数据库中是典型的?

if exists(select 1 from tosync where tbname = "%s" and tbid = %d 
   and (act = 1 and %d = 3 or act = 3 and %d = 1)
begin
  delete from tosync where tbname = "%s" and tbid = %d 
end
else
begin
  insert into tosync(tbname, tbid, act) values("%s", %d, %d);
end

替换值分别为

 [TbName, tbid, act, act, TbName, tbid, TbName, tbid, act]

请注意,本主题与 UPSERT 和 sqlite 中的类似问题无关。

您不能在 SQLite 中以这种方式进行条件查询。

但是您可以执行 INSERT ... WHERE NOT EXISTS ...

查看此以获取更多信息...http://www.sqlite.org/lang_insert.html

一段时间后,针对这种特殊情况找到了解决方案。

我必须 运行 在具有相同条件的行中同时插入和删除查询:

insert or replace into tosync (tbname,tbid,act) 
  select                        "%s" ,%d  ,%d 
where not exists(select 1 from tosync 
                 where tbname="%s" and tbid=%d and (act=1 and %d=3 or act=3 and %d=1));

delete from tosync 
where tbname="%s" and tbid=%d and exists(select 1 from
          tosync where tbname="%s" and tbid=%d and (act=1 and %d=3 or act=3 and %d=1));