gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:31:33 25 4
gpt4 key购买 nike

我正在尝试从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com