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

Beh*_*m-s 6 sql sqlite if-statement upsert

我无法使用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
Run Code Online (Sandbox Code Playgroud)

它说"IF附近的语法错误"如何解决问题

jkl*_*ack 6

SQLite没有IF语句(请参阅支持的查询列表)

Insetad,请查看ERIC B对另一个主题的建议.您正在有效地查看执行UPSERT(如果记录存在则更新,如果不存在则INSERT).Eric B.有一个很好的例子,说明如何在SQLite语法中使用SQLite中的"INSERT OR REPLACE"功能.基本上,你做的事情如下:

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