Saa*_*mar 2 sqlite iphone objective-c ios
我正在使用此代码从我的ipad应用程序的数据库中删除一行,
-(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;
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用,有人可以指导我吗?
你的方法返回YES吗?
有几件事:
sqlite3_errmsg任何故障sqlite3_finalize是sqlite3_step回报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;
}
Run Code Online (Sandbox Code Playgroud)
假设此方法始终返回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;
}
Run Code Online (Sandbox Code Playgroud)
然后将诊断消息放入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;
}
Run Code Online (Sandbox Code Playgroud)