How to insert if not exists, or increase if exists in SQLite?

China☆狼群 提交于 2019-12-02 08:36:10
CaveCoder

You can use insert or replace.

I thinks this will do the trick

INSERT OR REPLACE INTO Test (Id,Val) 
  VALUES (  1,          
            COALESCE((SELECT Val + 1 FROM Test WHERE id = 1), 1)
          );
INSERT OR REPLACE INTO Test (Id,Val) 
  VALUES (  2,          
            COALESCE((SELECT Val + 1 FROM Test WHERE id = 2), 1)
          );

You just have to replace the numbers with your inputed id

Thanks Chico

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!