I don't see anything in the simplified code example above to explain the error message. The problem may be in another place (or, perhaps, in the process of simplifying the selection of code, you have eliminated the source of the problem).
What was said, I had several reactions:
You said:
I tried to set the result of this query to "BOOL Success" and print the results, and I got YES
I do not know how this is possible, because executeQuery returns FMResultSet not a BOOL . This is a moot point, though, because your ALTER TABLE statement must be executed with executeUpdate not executeQuery . (The latter should work, but the former is more logical.)
I do not understand why you removed the error check. This is the best way to identify problems.
Totally unrelated, but you should never use stringWithFormat when building SQL. You open yourself up for SQL crashes as a result of unexpected input (for example, what if one of your values ββhas double quotes inside it). Even worse, you are subjecting yourself to SQL injection attacks. Use instead ? placeholders in SQL, and then just pass the values ββas parameters to the executeUpdate (or executeQuery ) method.
So the following code worked fine for me:
BOOL success; FMDatabase *db = [FMDatabase databaseWithPath:databasePath]; if (![db open]) { NSLog(@"open failed"); return; } if (![db columnExists:@"placeName" inTableWithName:@"MYPLACES"]) { success = [db executeUpdate:@"ALTER TABLE MYPLACES ADD COLUMN placeName TEXT"]; NSAssert(success, @"alter table failed: %@", [db lastErrorMessage]); } NSString *insertSQL = @"INSERT INTO MYPLACES (placeID, placeName) VALUES (?, ?)"; success = [db executeUpdate:insertSQL, placeIDValue, placeNameValue]; NSAssert(success, @"insert failed: %@", [db lastErrorMessage]); [db close];
Since I donβt see what might cause the problem you are describing, I can only offer you one step through this code in your debugger and make sure that it matches your path.
source share