I am using this code to delete a row from the database for my ipad application,
-(BOOL) removeSegmentWithSegmentId:(NSInteger)sId
{
AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
sqlite3_stmt *statement;
NSString *removeKeyword =[NSString stringWithFormat:@"DELETE FROM segment WHERE segment.segment_id = %d",sId];
const char *query = [removeKeyword UTF8String];
NSLog(@"%@",removeKeyword);
//if(sqlite3_prepare_v2(appDelegate->globalConnection,[removeKeyword UTF8String] , -1, &statement, NULL) == SQLITE_OK)
if(sqlite3_prepare_v2(appDelegate->globalConnection,query , -1, &statement, NULL) == SQLITE_OK)
{
if(sqlite3_step(statement) == SQLITE_DONE) {
sqlite3_finalize(statement);
return YES;
}
}
return NO;
}
but it is not working, can anyone guide me please?
Best Answer
Is your method returning YES?
A couple of things:
sqlite3_errmsg
on any failuressqlite3_finalize
issqlite3_step
returnsSQLITE_DONE
, whereas you really should be doing it whenever you successfully didsqlite3_prepare_v2
So, I might suggest, at a minimum:
Assuming this method was always returning
YES
, if you're not seeing records deleted, it must be that it's not finding a record to delete. (That is not considered a SQLite failure. The SQL was successfully executed, but theWHERE
clause couldn't be satisfied.) You can verify this by defining the following method:And then put the diagnostic message in
removeSegmentWithSegmentId
: