1.表不存在则创建 create table if not exists student( _id Integer primary key autoincrement , name Text , age Integer) 2.当表中的数据不存在时插入数据 insert into student(name,age) select 'zz7zz7zz',25 where not exists(select * from student where name='zz7zz7zz' and age=25) 重复执行多次,仍然只有一条数据
[查看图片] 3.当表中的数据不存在则插入,存在则更新 (使用REPLACE 语句) 这时候的创建表的sql语句为:create table if not exists student( _id Integer primary key , name Text , age Integer) 不存在插入,存在更新sql语句为:replace into student( _id , name ,age ) VALUES ( 1,'zz7zz7zz',25) ,重复执行多次,仍然只有一条数据
[查看图片] 将年龄改为35,发现数据表没有插入新数据,只是更新了 replace into student( _id , name ,age ) VALUES ( 1,'zz7zz7zz',35)
[查看图片] 将学生id(主键)改为2,则发现插入了一条新数据 replace into student( _id , name ,age ) VALUES ( 2,'zz7zz7zz',35)