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);

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