1. 程式人生 > >android sqlite 不存在插入,存在更新語句

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/liping13599168/archive/2011/05/24/2054908.html

郵箱[email protected]

微博