sqlite sql语句搞定重复插入的问题

1.这种写法无关主键

Insert or Ignore into tableName(col1,col2......) select  '{0}','{1}'...

where not exists

(select  col1,col2......  from tableName where col1='{0}' and col2='{1}' ....)

2.下面这种写法field1为主键

假设你的表叫:yourtable,其有二个字段:field1, field2(假设都是 int 型,field1 为主键)

如果不存在就插入,存在就忽略的方式,用 insert or ignore:

INSERT OR IGNORE INTO yourtable (field1,field2) VALUES(1,2)

或者如果不存在就插入,存在就更新的方式,用 insert or replace:

INSERT OR REPLACE INTO yourtable (field1,field2) VALUES(1,2);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章