几天来我一直试图弄清楚这一点,但我无法弄清楚发生了什么。在我的应用程序中,我希望能够从 tableView 中删除一个项目和相关项目。因此,当按下 TableView 中的删除按钮时,会调用以下方法。
- (BOOL) deleteDuik:(MOODuik *) duik
{
int diveid = [duik duikId];
NSLog(@"Begin Delete");
sqlite3 *db = [self openDatabase];
sqlite3_stmt * deleteStmt = nil;
BOOL succes = TRUE;
@try{
if (db != NULL) {
sqlite3_exec(db, "BEGIN TRANSACTION", 0, 0, 0);
//NSString *sqlString = [NSString stringWithFormat:@"delete from duik where duikid = %d", diveid];
const char *sql = "delete from duik where duikid = ?";
if(sqlite3_prepare_v2(db, sql, -1, &deleteStmt, NULL) == SQLITE_OK)
{
sqlite3_bind_int(deleteStmt, 1, diveid);
sqlite3_step(deleteStmt);
sqlite3_finalize(deleteStmt);
deleteStmt = nil;
sql = "delete from waarneming where duikid = ?";
if(sqlite3_prepare_v2(db, sql, -1, &deleteStmt, NULL) == SQLITE_OK)
{
sqlite3_bind_int(deleteStmt, 1, diveid);
sqlite3_step(deleteStmt);
sqlite3_finalize(deleteStmt);
}
}
} else
{
NSLog(@"Failed to connect to db");
succes = FALSE;
}
if(succes && ( [duik backendid] == 0 || [self deleteDuikServerSide:[duik backendid]]))
{
NSLog(@"End Delete");
if(sqlite3_exec(db, "COMMIT TRANSACTION", 0, 0, 0) !=SQLITE_OK) NSLog(@"SQL Error: %s",sqlite3_errmsg(db));
} else{
NSLog(@"Failed to delete remote copy");
sqlite3_exec(db, "ROLLBACK TRANSACTION", 0, 0, 0);
succes = false;
}
} @catch (NSException *e) {
NSLog(@"Failed to delete: %@", [e description]);
sqlite3_exec(db, "ROLLBACK TRANSACTION", 0, 0, 0);
} @finally {
@try {
sqlite3_finalize(deleteStmt);
sqlite3_close(db);
}
@catch (NSException *exception) {
//REally nothing to do here
}
sqlite3_close(db);
}
return succes;
}
当我尝试运行它时,它一直告诉我数据库已锁定,但我不知道它为什么被锁定。我已检查其他操作是否同时需要数据库,但我找不到任何操作。此外,如果我从同一个 tableView 添加一行,我将使用相同的构造和事务,并且该构造确实有效。我已经尝试了我能想到的所有方法,至少重写了这段代码 10 次,但到目前为止还没有运气。
有没有人可以告诉我我在这里做错了什么?