无法从sqlite中删除表视图中的行数据

时间:2014-12-18 00:11:56

标签: ios sqlite uitableview

我正在尝试从表格视图中删除数据。数据来自数据库。

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];

}

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

1 个答案:

答案 0 :(得分:1)

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