【发布时间】:2013-08-16 01:04:20
【问题描述】:
我已经使用 SQLite 数据库在我的 tableview 中加载了一些数据,格式为 1) 一些值,2) 一些值等等。 1 和 2 是主键,这是我在表格视图的删除功能上滑动的删除代码
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[self openDB];
NSString *idofTable = [NSString new];
NSScanner *scanner = [[NSScanner alloc] initWithString:[self.tableLogView cellForRowAtIndexPath:indexPath].textLabel.text];
[scanner scanUpToString:@")" intoString:&idofTable];
NSLog(@"id=%@, text=%@", idofTable, [self.tableLogView cellForRowAtIndexPath:indexPath].textLabel.text);
if(editingStyle==UITableViewCellEditingStyleDelete)
{
char *error;
NSString *sql = [NSString stringWithFormat:@"DELETE FROM History WHERE 'id' = '%@'", idofTable];
NSLog(@"%@",sql);
if(sqlite3_exec(db, [sql UTF8String], NULL, NULL,&error)!=SQLITE_OK)
{
sqlite3_close(db);
NSAssert(0,@"Could not create table");
}
else
{
NSLog(@"Data Deleted");
sqlite3_exec(db, "COMMIT", NULL, NULL, &error);
}
}
[tableLogView reloadData];
}
“数据已删除”日志被打印,但数据不会在数据库或 tableview 中被删除。任何帮助表示赞赏。不知道哪里出错了,查询好像是正确的。
【问题讨论】:
-
请把sql语句的Log贴在这里。身份证正确吗?
-
你怎么能说数据没有被删除,通过重新加载tableview?你确定 tableview 的值已经更新了吗?
-
您还应该从表格数据数组中删除记录以刷新表格视图。
-
@gamerlegend 此评论与您的问题无关。我建议您使用FMDB Framework 进行所有数据库操作。你可以参考这个答案stackoverflow.com/a/17268032/1017893
-
感谢 kapil 的代码,我发现当我删除引号 'id' 作为 id 时,我的删除语句有效。我做了插入和所有使用引号现在在使用引号时有点困惑。
标签: ios sqlite sql-delete