I’m using FMDB to interface with an SQLite database. I have it inserting rows fine, but when I try and UPDATE one of them, the UPDATE does not occur, but no error is reported.
Here’s what I’m doing:
userHistoryDB.logsErrors = YES;
userHistoryDB.traceExecution = YES;
NSString *query = [NSString stringWithFormat:
@"INSERT OR IGNORE INTO phrase_history (code, %@) VALUES (\"%@\", 0); "
@"UPDATE OR FAIL phrase_history SET %@ = %@ + 1 WHERE code = \"%@\";",
countTypeColumn,
phraseCode,
countTypeColumn,
countTypeColumn,
phraseCode];
BOOL rc = [userHistoryDB executeUpdate:query];
rc is YES, to indicate nothing is wrong. Here is an example call:
<FMDatabase: 0x5303af0> executeUpdate: INSERT OR IGNORE INTO phrase_history (code, presented) VALUES ("grapefruit", 0); UPDATE OR FAIL phrase_history SET presented = presented + 1 WHERE code = "grapefruit";
As far as FMDB is concerned, this has executed fine. However, the record does not increment. There is nothing wrong whatsoever with the SQL – if I paste it into the sqlite3 command line tool, it runs perfectly.
Any ideas as to why it’s not running? Things I have tried to no avail:
- Enclosing the line in a transaction
- Running using executeQuery
- Running just the update (with the entry already manually inserted).
- Slamming head against desk.
You have two sqlite statements there, and FMDB won’t automatically convert your single compound line into two separate statements. You need to run them separately, which is what the sqlite3 command line tool is doing.