"数据库被锁定"在我的sqlite中删除操作

时间:2013-05-23 07:28:48

标签: iphone ios ios6

我的应用程序中有一个删除表行的函数,但我有一个错误,说“数据库被锁定”。

- (BOOL) deleteByCaption:(NSString *)registerCaption
 {
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK) {
    NSString *querySQL = [NSString stringWithFormat:@"DELETE FROM tabelA WHERE CONTENT_CAPTION = \"%@\"", registerCaption];

    const char *query_stmt = [querySQL UTF8String];
    //        NSMutableArray *resultArray = [[NSMutableArray alloc] init];
    if(sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) == SQLITE_OK){
        if(sqlite3_step(statement) == SQLITE_ROW){
            return YES;
        }else{
            NSLog(@"Error while creating delete statement. '%s'", sqlite3_errmsg(database));
            return NO;
        }
    }
    sqlite3_reset(statement);
}
return NO;
}

2 个答案:

答案 0 :(得分:0)

您缺少sqlite3_finalize()。请看以下代码中的// PUT HERE部分。

- (BOOL) deleteByCaption:(NSString *)registerCaption
{
    BOOL isDeleted = NO; //EDIT
    const char *dbpath = [databasePath UTF8String];
    if (sqlite3_open(dbpath, &database) == SQLITE_OK) {
        NSString *querySQL = [NSString stringWithFormat:@"DELETE FROM tabelA WHERE CONTENT_CAPTION = \"%@\"", registerCaption];

        const char *query_stmt = [querySQL UTF8String];
        //        NSMutableArray *resultArray = [[NSMutableArray alloc] init];
        if(sqlite3_prepare_v2(database, query_stmt, -1, &statement, NULL) == SQLITE_OK){
            if(sqlite3_step(statement) == SQLITE_ROW){
                isDeleted = YES;
                //return YES; //EDIT
            }else{
                NSLog(@"Error while creating delete statement. '%s'", sqlite3_errmsg(database));
                //return NO; //EDIT
            }
        }
        sqlite3_reset(statement);
        sqlite3_finalize(statement); //PUT HERE
        sqlite3_close(database); //EDIT
    }
    //return NO; //EDIT
    return isDeleted; //EDIT
}

编辑:

尝试设置一个变量返回。 查看上面代码中的已编辑部分。搜索//编辑。

答案 1 :(得分:0)

deleteByCaption函数中,您正在从函数返回而不关闭数据库。

在撰写"RETURN YES""RETURN NO"

之前,您应该写下以下陈述
sqlite3_finalize(statement);
sqlite3_close(database);

请检查您使用sqlite3_open()功能的所有功能。

并确保在从该函数返回之前编写sqlite3_close()函数。