【问题标题】:Data not getting deleted from database using SQLite使用 SQLite 未从数据库中删除数据
【发布时间】: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


【解决方案1】:

试试这个

 if(editingStyle==UITableViewCellEditingStyleDelete)
        {
            NSString *dbFilePath =[DBclass getDBPath];

            sqlite3_stmt *deleteStmt = nil;
         if(sqlite3_open([dbFilePath UTF8String], &database)==SQLITE_OK)
        {

        NSString *deleteString=[NSString stringWithFormat:@"delete from table where ID =%d",idValue];
        const char *sql =[deleteString UTF8String];

        if(sqlite3_prepare_v2(database, sql, -1, &deleteStmt, NULL) != SQLITE_OK)
            NSAssert1(0, @"Error while creating delete view statement. '%s'", sqlite3_errmsg(database));

        if(SQLITE_DONE != sqlite3_step(deleteStmt))
            NSAssert1(0, @"Error while deleting data. '%s'", sqlite3_errmsg(database));
        else
            NSLog(@"Success in deleting the medicine.");

        sqlite3_close(database);
    }
        }
        [tableLogView reloadData];

【讨论】:

  • Na 代码不对,他甚至没有完成也没有关闭数据库 ;)
  • 谢谢,实际上,当我从“id”中删除引号时,我的代码现在也可以工作了,这要感谢您的代码帮助我找出错误。数据库中的值现在被删除,但即使我调用 reloadData,它也不会更新表视图。有什么想法吗??
  • 是的,在从数据库中删除之后,您还要做一件事,然后在重新加载数据之前从数组索引中删除值 [yourArray removeObjectAtIndex:indexPath.row]; [表重载数据];
  • 试过但抛出错误,无论如何我通过在 tablerow 返回事件中重新填充我的数据库来解决它。现在删除所有数据时出现错误,我的 sql stmt 是 DELETE * FROM History。它抛出断言失败错误。有什么想法吗??
  • 兄弟当你触发删除语句时不要使用 * 试试这个 DELETE FROM History
【解决方案2】:

您既没有完成也没有关闭数据库。

sqlite3_finalize(stmt);

sqlite3_close(DB);

【讨论】:

    【解决方案3】:

    这是删除一行的正确方法:

    if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
    
       NSString *sql = [NSString stringWithFormat:@"DELETE FROM History WHERE 'id' = '%@'", idofTable];
    
       const char *del_stmt = [sql UTF8String];
       sqlite3_prepare_v2(contactDB, del_stmt, -1, & deleteStmt, NULL);
       if (sqlite3_step(deleteStmt) == SQLITE_DONE)
       {
           NSLog(@"Data Deleted");
           sqlite3_exec(db, "COMMIT", NULL, NULL, &error);
       }else {
           NSLog( @"Error: %s", sqlite3_errmsg(database) );
       }
       sqlite3_finalize(deleteStmt);
       sqlite3_close(contactDB);
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-17
      • 2013-05-28
      • 1970-01-01
      • 2011-08-24
      • 2017-02-28
      相关资源
      最近更新 更多