打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
人性潜规则:1、记住:先敬罗衣后敬人,先敬皮囊后敬魂
二战电影:那瓦龙敢死队
填词 写曲 作诗 应试时用平水韵----词林正韵(5)
杜甫七律《郑驸马宅宴洞中》读记
淡定从容,乐观积极的女性,绝对是女人中的精品,极品
人性铁律,处世智慧!
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服