Two observations:
You should examine the
lastErrorMessage
of the database if you have an error. It helps if you store the database pointer in a separate variable if you're going to do multiple calls to the database before closing it.You definitely don't want to call
[TRSharedLocalDatabase openDatabase]
multiple times for one session with your database. Or you could refactor it to conform to a singleton pattern.Ideally, you should use
?
placeholders in your SQL withexecuteUpdate
method, not printf-style placeholders withexecuteUpdateWithFormat
(see the warning in the executeUpdateWithFormat documentation). If not, your text fields with characters that need to be escaped (e.g. quotation mark) won't be. (This also protects you against SQL injection attacks.)
Thus:
FMDatabase *database = [TRSharedLocalDatabase openDatabase];
if (!database) {
NSLog(@"Unable to open database");
return;
}
if([database executeUpdate:@"INSERT INTO event (title,date,address,latitude,longitude,location,facebookID,picPath,description) VALUES (?,?,?,?,?,?,?,?,?)",event.title ,event.date, event.address, [NSNumber numberWithDouble:event.geoPoint.longitude], [NSNumber numberWithDouble:event.geoPoint.latitude], event.location.objectId, event.facebookID ,picPath ,event.description]) {
NSLog(@"Ok");
} else {
NSLog(@"Not Ok: %@", [database lastErrorMessage]);
}
[database close];