I’m trying to perform an update command to my sqlite3 table. But it’s not working:
+(void)updateContact:(Contact *)c withOriginalFirst:(NSString *)originalFirst originalLast:(NSString *)originalLast originalBriefDescription:(NSString *)originalBriefDescription {
sqlite3 *database;
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
NSString *cmd = [NSString stringWithFormat:@"update contacts set first='%@', last='%@', briefDescription='%@' where first='%@' and last='%@' and briefDescription='%@';",
[c first],[c last],[c briefDescription],originalFirst,originalLast,originalBriefDescription];
const char * sql = [cmd UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, sql, -1, &compiledStatement, NULL) == SQLITE_OK) {
NSLog(@"updateContact SUCCESS - executed command %@",cmd);
}
else {
NSLog(@"updateContact FAILED - failed to execute command %@",cmd);
}
sqlite3_finalize(compiledStatement);
}
else {
NSLog(@"pdateContact FAILED - failed to open database");
}
sqlite3_close(database);
NSLog(@"After update, contacts = %@",[SQLMaster getContactsFromDatabase]);
}
I see “updateContact SUCCESS – executed command …” printed. But the table is not being updated. What am I doing wrong?
A few things:
Where is your databasePath pointing? Be sure it is pointing to the documents path in the bundle, and not an external db in the project directly. Also, ensure you put the step statement correctly.
Also, I see from the line [SQLMaster getContactsFromDatabase] that you are calling other database methods. Be sure that sqlite3_finalize is being reached on all of these database calls, or the database may not release the busy handler.