SQLite中的条件查询

时间:2017-03-25 02:08:40

标签: sqlite exists

在sqlite中做什么在大多数DB中都是典型的?

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中可用的类似问题无关。

2 个答案:

答案 0 :(得分:0)

您无法在SQLite中以这种方式执行条件查询。

然而,您可以执行INSERT ... WHERE NOT EXISTS ...

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

答案 1 :(得分:0)

在这种特殊情况下找到了一段时间的解决方案。

我必须在具有相同条件的行中运行insert和delete查询:

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));