我正在尝试从表格视图中删除数据。数据来自数据库。
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];
}
在这里,当我点击删除时,我得到"语句数据已成功删除"但是,当我在删除操作后检查数据库文件时,数据仍然存在。有人可以告诉我这里有什么问题吗?
答案 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");
}
}
参考: