这篇文章主要介绍了SQLite 实现if not exist 类似功能的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧。
需要实现:
if not exists(select * from ErrorConfig where Type=‘RetryWaitSeconds’)
begin
insert into ErrorConfig(Type,Value1)
values(‘RetryWaitSeconds’,‘3’)
end
只能用:
insert into ErrorConfig(Type,Value1)
select ‘RetryWaitSeconds’,‘3’
where not exists(select * from ErrorConfig where Type=‘RetryWaitSeconds’)
因为 SQLite 中不支持SP
补充:sqlite3中NOT IN 不好用的问题
在用sqlite3熟悉SQL的时候遇到了一个百思不得其解的问题,也没有在google上找到答案。虽然最后用“迂回”的方式碰巧解决了这个问题,但暂时不清楚原理是什么,目前精力有限,所以暂时记录下来,有待继续研究。
数据库是这样的:
CREATE TABLE book (
id integer primary key,
title text,
unique(title)
);
CREATE TABLE checkout_item (
member_id integer,
book_id integer,
movie_id integer,
unique(member_id, book_id, movie_id) on conflict replace,