如果iphone sqlite中已存在记录,则阻止插入

时间:2013-02-15 13:26:51

标签: iphone objective-c ios6 sqlite

我想在iphone中的sqlite表中插入一条记录,如果它还没有。

我使用了三个名为

的字段
  • id(integer primary key Auto increment)

  • name(text)

  • address(text)

如果表中存在名称,则不应插入任何行。

我该怎么做?

2 个答案:

答案 0 :(得分:2)

试试这个(source):

INSERT OR IGNORE INTO TableName (Field) VALUES 'value'

答案 1 :(得分:1)

将字段name设为UNIQUEKEY,其余部分将由sqlite本身进行处理。

这是Create table query:

  

CREATE TABLE YourTableName(id integer NOT NULL PRIMARY KEY AUTOINCREMENT DEFAULT 0,name text UNIQUE,address text)