1

Im trying to insert some values to my sqlite database. The db is already of the doc folder on the phone. I cant figure out what is going wrong. I set trace executing but the db tells me that it does not have any error. Can someone help me?

if([[TRSharedLocalDatabase openDatabase] executeUpdateWithFormat:@"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");  
}  

+(FMDatabase *)openDatabase {  
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];  
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"4PartyLocalSystem.sqlite"];  
    **FMDatabase *database = [FMDatabase databaseWithPath:writableDBPath];**  
    [database open];  
    [database setTraceExecution:YES];  
    return database;   
}

2013-08-06 13:21:42.499 4Party[13018:907] executeUpdate: INSERT INTO event (title,date,address,latitude,longitude,location,facebookID,picPath,description) VALUES (?,?,?,?,?,@,?,?,?)

12
  • 2
    Rafael, it would be helpful if you provided more information about the TRSharedLocalDatabase. Can you share the code for the method openDatabase? It will be difficult to help you without knowing more about what that code is doing, exactly. Commented Aug 6, 2013 at 16:01
  • The openDatabase method is just for opening the DB. Here it is:NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"4PartyLocalSystem.sqlite"]; FMDatabase *database = [FMDatabase databaseWithPath:writableDBPath]; [database open]; [database setTraceExecution:YES]; return database; Commented Aug 6, 2013 at 16:04
  • 1
    Please add the code to the question by editing it rather than add it in a comment. It is virtually impossible to read your code in the comment Commented Aug 6, 2013 at 16:07
  • Thanks, can you put that in the original post? Also add the code for the databaseWithPath:. You will also want to consider adding some error handling to your code on the SQLite operations. That will give you more information about specific errors. Commented Aug 6, 2013 at 16:07
  • I tried logging the sql errors, but the db always return me "not an error". @Aaron Commented Aug 6, 2013 at 16:11

1 Answer 1

1

Two observations:

  1. 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.

  2. Ideally, you should use ? placeholders in your SQL with executeUpdate method, not printf-style placeholders with executeUpdateWithFormat (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];
Sign up to request clarification or add additional context in comments.

1 Comment

THANKSSS!!! I stored the FMDatabase in a variable and then the DB could log the lastErrorMessage!! Now i fixed the query!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.