我想通过Tableview删除数据库中的行。 在我的应用程序中,我使用SKDatabase文件进行查询。现在,当我调用删除查询时,它不起作用。 下面是我的代码
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
i = indexPath.row;
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[objappdel.sk deleteWhere:[NSString stringWithFormat:@"ID = %d",indexPath.row] forTable:@"Highlights_Table"];
tablstr = [arrayFinal objectAtIndex:indexPath.row];
NSLog(@" %@",tablstr);
[arrayFinal removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
[table1 reloadData];
}
如果我使用删除所有数据查询然后它正在工作,我不知道如何解决这个问题。请帮忙。
删除SKDatabase.m中的方法代码
- (void)deleteWhere:(NSString *)where forTable:(NSString *)table
{
NSString *sql = [NSString stringWithFormat:@"DELETE FROM %@ WHERE %@",
table,where];
NSLog(@" %@",sql);
[self runDynamicSQL:sql forTable:table];
}
- (BOOL)runDynamicSQL:(NSString *)sql forTable:(NSString *)table
{
int result;
NSAssert1(self.dynamic == 1,[NSString stringWithString:@"Tried to use a dynamic function on a static database"],NULL);
sqlite3_stmt *statement;
if (statement = [self prepare:sql])
{
result = sqlite3_step(statement);
}
sqlite3_finalize(statement);
if (result)
{
if (self.delegate != NULL && [self.delegate respondsToSelector:@selector(databaseTableWasUpdated:)])
{
[delegate databaseTableWasUpdated:table];
}
return YES;
NSLog(@"Done");
}
else
{
return NO;
}
}
- (sqlite3_stmt *)prepare:(NSString *)sql
{
const char *utfsql = [sql UTF8String];
NSLog(@" %s",utfsql);
sqlite3_stmt *statement;
if (sqlite3_prepare([self dbh],utfsql,-1,&statement,NULL) == SQLITE_OK)
{
return statement;
}
else
{
return 0;
}
}
答案 0 :(得分:0)
通常来自SQLite
的对象为 NSNumber
,因此您应该替换 "ID = %d"
的 "ID == %@",[NSNumber numberWithInteger:indexPath.row];
强>
我从未使用SKDatabase,但我非常确定“ID =%d”不会生成规则,只有"="
,如果 { {1}} 无效,您应尝试使用 "ID == %@"
。
答案 1 :(得分:0)
尝试使用引号:
[objappdel.sk deleteWhere:[NSString stringWithFormat:@"ID = \"%d\"",indexPath.row] forTable:@"Highlights_Table"];
而不是:
[objappdel.sk deleteWhere:[NSString stringWithFormat:@"ID = %d",indexPath.row] forTable:@"Highlights_Table"];
答案 2 :(得分:0)
Highlights_Table *tableObj = [arrayFinal objectAtIndex:indexPath.row];
[[DataManager mangedObjectContext] deleteObject:tableObj];
[[DataManager mangedObjectContext] save:&error];
[arrayFinal removeObjectAtIndex:indexPath.row];
[self.tableView reloadData];