ios - 无法从 sqlite 中删除我的 TableView 中的行数据

标签 ios sqlite uitableview

我正在尝试从 TableView 中删除数据。数据来自数据库。

if (editingStyle == UITableViewCellEditingStyleDelete) {

    DetailData *datatoDelete;//=[DetailData new];
    datatoDelete= dataArray[indexPath.row];

    NSLog(@"ID at  selected row is %@",datatoDelete.ID);


    NSString *docsDir;
    NSArray *dirPaths;

    // Get the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir = dirPaths[0];

    // Build the path to the database file

    databasePath = [[NSString alloc]
                    initWithString: [docsDir stringByAppendingPathComponent:
                                     @"MYDat.db"]];

    const char *dbpath = [databasePath UTF8String];
    sqlite3_stmt    *statement;

    if (sqlite3_open(dbpath, &myDatabase) == SQLITE_OK)
    {

        NSString *querySQL = [NSString stringWithFormat:@"DELETE FROM ABC WHERE ID=%@",datatoDelete.ID];

    // i tried ID='%@' too

        const char *query_stmt = [querySQL UTF8String];

        if (sqlite3_prepare_v2(myDatabase, query_stmt, -1, &statement, NULL) == SQLITE_OK)
        {

            NSLog(@"statement deleted successfully"); // i am seeing this statement when i click delete

        }
        sqlite3_finalize(statement);
        sqlite3_close(myDatabase);
    }

 //  [dataArray removeObjectAtIndex:indexPath.row];

    [tableView reloadData];

}

这里当我点击删除时,我得到“语句数据删除成功”但是,当我在删除操作后检查数据库文件时,数据仍然存在。谁能告诉我这里有什么问题吗?

最佳答案

sqlite3_prepare_v2只准备一条语句,需要使用sqlite3_step来执行。

  if (sqlite3_prepare_v2(myDatabase, query_stmt, -1, &statement, NULL) == SQLITE_OK)
  {
      if(sqlite3_step(statement) == SQLITE_DONE)
      {
           NSLog(@"statement deleted successfully");
      }
  }

供引用:

  1. > sqlite_step
  2. > Evaluate An SQL Statement

关于ios - 无法从 sqlite 中删除我的 TableView 中的行数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27537133/

相关文章:

ios - UIPrintInteractionController 无边框打印

ios - Firebase Analytics for iOS — 具有预定义事件的自定义参数 (Swift)

Android,SQLite rawQuery 在选择 ListView 中的项目后不发送任何结果

php - 使用插入CI的数据库锁定错误

ios - 在 UITableView 自定义单元格中的元素上点击手势

ios - 如何减少 UITabBarItem 图像和文本之间的垂直填充?

iphone - Segue 后属性被设置为 null

django - 如果在生产和开发中使用不同的 RDBMS(例如 PostgreSQL),什么时候不应该在 Django 中使用 SQLite 进行测试?

ios - 点击 UITableViewCell 时应用程序崩溃

ios - 使用自定义 UITableView subview