接近IF语法错误(SQLite3 + Objective-C)(如果不存在,则更新)

时间:2018-06-19 12:18:07

标签: ios sql sqlite

我在尝试执行以下代码时遇到错误[logging] near "IF": syntax error

NSString * saveQuery = [[NSString alloc] initWithFormat:@"IF (NOT EXISTS (SELECT * FROM USERDB WHERE email=\"%@\")) BEGIN INSERT INTO USERDB (firstName, lastName, username, email, password) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\") END ELSE BEGIN UPDATE USERDB (firstName, lastName, username, email, password) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\") WHERE email = \"%@\" END", email, firstName, lastName, username, email, password, firstName, lastName, username, email, password, email];
[userDB saveData:saveQuery];

如果用户似乎基于其电子邮件地址似乎不存在,我将尝试插入数据库(否​​则,我想更新其现有数据)。

1 个答案:

答案 0 :(得分:0)

更新!发现了INSERT or REPLACE

的美好世界
NSString * saveQuery = [[NSString alloc] initWithFormat:@"INSERT OR REPLACE INTO USERDB (firstName, lastName, username, email, password) VALUES (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\")", firstName, lastName, username, email, password];