android sqlite 不存在插入,存在更新语句

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)
  

  引用文章:http://www.cnblogs.com/liping135 ... /05/24/2054908.html


  原文链接:http://www.eyeandroid.com/thread-16575-1-2.html
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值