加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

SQLite中的IF语句:更新还是插入?

发布时间:2020-12-12 23:40:41 所属栏目:百科 来源:网络整理
导读:我无法使用SQLite运行此查询 if 0(select COUNT(*) from Repetition where (Word='behnam' and Topic='mine'))begin update Repetition set Counts=1+ (select Counts from Repetition where (Word='behnam' and Topic='mine'))endelsebegin insert Repetiti
我无法使用SQLite运行此查询
if 0<(select COUNT(*) from Repetition where (Word='behnam' and Topic='mine'))
begin
 update Repetition set Counts=1+ (select Counts from Repetition where (Word='behnam' and Topic='mine'))
end
else
begin
    insert Repetition(Word,Topic,Counts)values('behnam','mine',1)
end

它说“IF附近的语法错误”
我该如何解决这个问题

SQLite没有IF语句( see the list of supported queries)

Insetad,查看ERIC B关于另一个thread的建议.你实际上是在做一个UPSERT(如果记录存在则更新,如果不存在则INSERT). Eric B.有一个很好的例子,说明如何在SQLite语法中使用SQLite中的“INSERT OR REPLACE”功能.基本上,你做的事情如下:

INSERT OR REPLACE INTO Repetition (Word,Counts)    
VALUES (  'behnam',coalesce((select Counts + 1 from Repetition 
                   where Word = 'behnam',AND Topic = 'mine),1)
       )

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读