I see nothing in the above simplified code sample to explain the error you report. The problem may rest elsewhere (or, perhaps in the process of simplifying the code sample, you eliminated the source of the problem).
That having been said, I had a couple of reactions:
You said:
I tried to set the result of this query in "BOOL Success" and print the results and I got YES
I don't know how that's possible, because
executeQuery
returns aFMResultSet
not aBOOL
. This is moot, though, because yourALTER TABLE
statement should be performed withexecuteUpdate
notexecuteQuery
. (The latter should still work, but the former is more logical.)I don't understand why you removed the error checking. That's the best way to identify issues.
Completely unrelated, but you should never use
stringWithFormat
when building your SQL. You open yourself up to SQL failures resulting from unexpected input (e.g. what if one of your values had double quotes inside it). Worse, you expose yourself to SQL injection attacks. Instead, use?
placeholders in the SQL and then just pass the values as parameters to theexecuteUpdate
(orexecuteQuery
) 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 would cause the problem you describe, I can only suggest you single-step through this code in your debugger and make sure it's following the path you think it is.