android sqlite 不存在插入,存在更新语句
16lz
2021-01-25
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)
邮箱:zz7zz7zz@163.com
微博 :http://weibo.com/u/3209971935更多相关文章
- “罗永浩抖音首秀”销售数据的可视化大屏是怎么做出来的呢?
- Nginx系列教程(三)| 一文带你读懂Nginx的负载均衡
- 不吹不黑!GitHub 上帮助人们学习编码的 12 个资源,错过血亏...
- Android(安卓)检测SD卡是否可用
- targetSdkVersion导致的Android运行时权限问题
- Android中的四大组件回顾
- android 调用系统相机拍照,并保存到SD卡
- 移动数据流程
- 2014.07.23(2) ——— android FragmentPagerAdapter的notifyDataS