在事务期间锁定SQLITE数据库

时间:2013-04-22 12:37:02

标签: objective-c sqlite transactions locked

我一直在努力解决这个问题,但我无法弄清楚发生了什么。在我的应用程序中,我希望能够从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;
}

当我尝试运行它时,它一直告诉我数据库已锁定,但我不知道为什么它被锁定。我已经检查过其他动作是否同时需要DB,但我找不到任何动作。此外,如果我从同一个tableView添加一行,我使用相同的结构与事务,并且一个工作。我已经尝试了所有我想到的东西,至少重写了这段代码至少10次,但到目前为止还没有运气。

有谁能告诉我这里做错了什么?

0 个答案:

没有答案