sqlite不存在记录则插入数据

  问题:如下图在Sqlite数据库中存在一张Student表,现需要向表中插入数据,如果不存在同名记录则插入,否则不进行插入操作。

  解答:利用not exists语句,如下:

insert into [Student]([Name], [Age]) select '周俊豪', 21 
where not exists (select * from [Student] where [Name]='周俊豪')  

  执行该语句,第一遍执行之后会将 周俊豪 这条数据插入,再次执行则不再进行插入操作了。

 

  文章同步更新:http://www.motyer.cn/Files/BlogDetails/sqlite-insert-into-where-not-exists.html

posted @ 2018-03-01 17:25  健谈IT  阅读(1717)  评论(0编辑  收藏  举报