你的方法返回YES吗?
有几件事:
- 始终登录
sqlite3_errmsg
任何故障
- 现在,您所做
sqlite3_finalize
的只是sqlite3_step
return SQLITE_DONE
,而只要您成功了,您就应该这样做sqlite3_prepare_v2
所以,我可能会建议,至少:
-(BOOL) removeSegmentWithSegmentId:(NSInteger)sId
{
BOOL success = NO;
AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
sqlite3_stmt *statement;
NSString *removeKeyword = [NSString stringWithFormat:@"DELETE FROM segment WHERE segment.segment_id = %d",sId];
if (sqlite3_prepare_v2(appDelegate->globalConnection, [removeKeyword UTF8String], -1, &statement, NULL) == SQLITE_OK)
{
if(sqlite3_step(statement) == SQLITE_DONE)
{
success = YES;
}
else
{
NSLog(@"%s: step not ok: %s", __FUNCTION__, sqlite3_errmsg(appDelegate->globalConnection));
}
sqlite3_finalize(statement);
}
else
{
NSLog(@"%s: prepare failure: %s", __FUNCTION__, sqlite3_errmsg(appDelegate->globalConnection));
}
return success;
}
假设这个方法总是返回YES
,如果你没有看到记录被删除,那一定是它没有找到要删除的记录。(这不被认为是 SQLite 失败。SQL 已成功执行,但WHERE
无法满足子句。)您可以通过定义以下方法来验证这一点:
- (NSInteger)countSegmentWithSegmentId:(NSInteger)sId
{
NSInteger count = 0;
AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
sqlite3_stmt *statement;
NSString *sql = [NSString stringWithFormat:@"SELECT segment_id FROM segment WHERE segment.segment_id = %d", sId];
if (sqlite3_prepare_v2(appDelegate->globalConnection, [sql UTF8String], -1, &statement, NULL) == SQLITE_OK)
{
while ((rc = sqlite3_step(statement)) == SQLITE_ROW)
count++;
sqlite3_finalize(statement);
}
else
{
NSLog(@"%s: prepare failure: %s", __FUNCTION__, sqlite3_errmsg(appDelegate->globalConnection));
return -1;
}
return count;
}
然后将诊断消息放入removeSegmentWithSegmentId
:
- (BOOL)removeSegmentWithSegmentId:(NSInteger)sId
{
BOOL success = NO;
NSInteger count = [self countSegmentWithSegmentId:sId];
NSLog(@"%s there are %d records with segment_id of %d", __FUNCTION__, count, sId);
AppDelegate *appDelegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
sqlite3_stmt *statement;
NSString *removeKeyword = [NSString stringWithFormat:@"DELETE FROM segment WHERE segment.segment_id = %d",sId];
if (sqlite3_prepare_v2(appDelegate->globalConnection, [removeKeyword UTF8String], -1, &statement, NULL) == SQLITE_OK)
{
if(sqlite3_step(statement) == SQLITE_DONE)
{
success = YES;
}
else
{
NSLog(@"%s: step not ok: %s", __FUNCTION__, sqlite3_errmsg(appDelegate->globalConnection));
}
sqlite3_finalize(statement);
}
else
{
NSLog(@"%s: prepare failure: %s", __FUNCTION__, sqlite3_errmsg(appDelegate->globalConnection));
}
return success;
}