FMDB:更新时数据库被锁定

时间:2014-01-19 10:23:07

标签: ios fmdb

我正在使用FMDB,当我想要返回一些值时,我正在使用FMDatabase,当我不需要返回任何值时,我FMDatabaseQueue。 当我添加到数据库并使用FMDatabaseQueue时我没有错误(除非我在删除数据库时创建新数据库,然后创建[没有要删除的数据库,因此它创建了一个新的一])。 问题是,当我使用相同的database FMDatabaseQueue更新- (void)updateTrapInDatabase:(FMDatabase*)db distance:(double)distance trapID:(int)trapID distanceToClosePoint:(double)distanceToClosePoint { NSString *statment = [NSString stringWithFormat:@"UPDATE %@ SET %@=?, %@=? WHERE %@=?", TRAP_TABLE, DISTANCE, DISTANCE_TO_CLOSE_POINT, ID]; BOOL update = [db executeUpdate:statment, @(distance), @(distanceToClosePoint), @(trapID)]; NSLog(@"Update trapID: %i, success: %@", trapID, update?@"YES":[NSString stringWithFormat:@"NO: %@", [db lastErrorMessage]]); } 时,我发生错误:

  

未知错误最终确定或重置语句(5:数据库是   锁定)

     

数据库查询:UPDATE traps_table SET dist = 34721.853417,   dist_to_close_point = 35734.768293 WHERE _id = 10

当我在数据库文件本身上尝试查询时,我没有错误,一切都很顺利。

以下代码:

- (void)UpdateDistanceToTrap_currentLocation:(CLLocation*)currentLocation old_location:(CLLocation*)old_location theAzimuth:(double)theAzimuth theSpeed:(double)theSpeed
{
    NSLog(@"UpdateDistanceToTrap()");

    __block double latColumn, lonColumn, idColumn, closePointLatColumn, closePointLonColumn, alarmDistanceColumn;
    __block int _id = -1, alarmDistance = -1;
    __block double distance;
    __block CLLocation *trap_location = nil;

    __block NSString *sqlStatment = [NSString stringWithFormat:@"SELECT * FROM %@", TRAP_TABLE];

    FMResultSet *query = [trapsDB.db executeQuery:sqlStatment];

    if ([query next]) {
        latColumn = [query doubleForColumn:LAT];
        lonColumn = [query doubleForColumn:LON];
        closePointLatColumn = [query doubleForColumn:CLOSE_POINT_LAT];
        closePointLonColumn = [query doubleForColumn:CLOSE_POINT_LON];
        idColumn = [query intForColumn:@"_id"];
        alarmDistanceColumn = [query intForColumn:ALARMDISTANCE];

        do {
            _id = [query intForColumn:@"_id"];
            alarmDistance = [query intForColumn:ALARMDISTANCE];

            if ([old_location distanceFromLocation:currentLocation] > kDISTANCE_THRESHOLD_FOR_CHECK_LOCATIONS) {
                trap_location = [[CLLocation alloc] initWithLatitude:[query doubleForColumn:LAT]
                                                           longitude:[query doubleForColumn:LON]];

                distance = [trap_location distanceFromLocation:currentLocation];

                if (distance < alarmDistance + [ref integerForKey:@"DISTANCE_OFFSET"]) {
                    [trapsDB enterOrOutFromArea_operation:kENTER andTrapID:_id];

                    [self startSaveOnMyLocations:currentLocation.coordinate.latitude
                                             lon:currentLocation.coordinate.longitude
                                            time:CreateDate()
                                           speed:theSpeed
                                         azimuth:theAzimuth
                                          trapID:_id];
                }
                else {
                    if ([trapsDB isEnterToArea_trapID:_id] == kENTER) {
                        [self startPostXml:_id];
                        [trapsDB enterOrOutFromArea_operation:kOUT andTrapID:_id];
                    }
                }
            }
            if ([query doubleForColumn:CLOSE_POINT_LAT] != 0.0 || [query doubleForColumn:CLOSE_POINT_LAT] != 0) {
                trap_location = [[CLLocation alloc] initWithLatitude:[query doubleForColumn:CLOSE_POINT_LAT]
                                                           longitude:[query doubleForColumn:CLOSE_POINT_LON]];

                double distanceToClosePoint = [trap_location distanceFromLocation:currentLocation];

                trap_location = [[CLLocation alloc] initWithLatitude:[query doubleForColumn:LAT]
                                                           longitude:[query doubleForColumn:LON]];

                distance = [trap_location distanceFromLocation:currentLocation];

                [trapsDB updateTrapInDatabase:trapsDB.db distance:distance trapID:_id distanceToClosePoint:distanceToClosePoint];
            }
            else {
                trap_location = [[CLLocation alloc] initWithLatitude:[query doubleForColumn:LAT]
                                                           longitude:[query doubleForColumn:LON]];

                distance = [trap_location distanceFromLocation:currentLocation];

                [trapsDB updateTrapInDatabase:trapsDB.db distance:distance trapID:_id distanceToClosePoint:distance];
            }
        } while ([query next]);
    }
}

引用函数的代码:

- (void)UpdateDistanceToTrap_currentLocation:(CLLocation*)currentLocation old_location:(CLLocation*)old_location theAzimuth:(double)theAzimuth theSpeed:(double)theSpeed;

#define trapsDB     ([SQLiteHandler sharedDatabase])

我的代码出了什么问题?

修改

TrapService.mm:

@property (strong, nonatomic) FMDatabase *db;

- (void)updateTrapInDatabase:(FMDatabase *)db distance:(double)distance trapID:(int)trapID distanceToClosePoint:(double)distanceToClosePoint;

SQLiteHandler.m:

{{1}}

修改-2: 更新了我的固定代码。

2 个答案:

答案 0 :(得分:2)

您正在进行inDatabase调用,在SELECT语句中进行迭代,然后尝试再进行inDatabase次呼叫。如果您要这样做,您可能希望将FMDatabase作为参数传递给方法,而不是再次调用inDatabase

- (void)updateTrapInDatabase:(FMDatabase *)db distance:(double)distance trapID:(int)trapID distanceToClosePoint:(double)distanceToClosePoint
{
    NSString *sqlStatment = [NSString stringWithFormat:@"UPDATE %@ SET %@=?, %@=? WHERE %@=?", TRAP_TABLE, DISTANCE, DISTANCE_TO_CLOSE_POINT, ID];

    BOOL update = [db executeUpdate:sqlStatment, @(distance), @(distanceTpClosePoint), @(trapID)];
    NSLog(@"Update trapID: %i, success: %@", trapID, update?@"YES":[NSString stringWithFormat:@"NO: %@", [db lastErrorMessage]]);
}

并将您的电话换成:

[trapsDB updateTrapDistance:distance andTrapID:_id andDistanceToClosePoint:distanceToClosePoint];

使用:

[trapsDB updateTrapInDatabase:db distance:distance trapID:_id distanceToClosePoint:distanceToClosePoint];

(顺便说一句,在SQL中使用?占位符通常是谨慎的(避免与float变量有精确相关的问题,避免SQL注入问题并引用NSString变量的问题)所以我也调整了上面的SQL。另外,如果SQL语句失败,我总是记录lastErrorMessage,否则我只是猜测SQL失败的原因。根据需要使用或忽略这些不相关的调整。)< / p>

但重点是,在inDatabase已经存在inDatabase时,永远不应该调用inDatabase。但令人好奇的是,在inDatabase中调用FMDatabaseQueue通常会导致死锁,而不是“数据库被锁定”错误。坦率地说,FMDatabaseQueue的整个目的是调解来自不同线程的数据库请求,以便数据库永远不会繁忙。如果您有多个FMDatabase或{{1}}对象,那么您通常会收到“数据库已锁定”错误的唯一时间。您的应用程序应该只在任何给定时间打开一个数据库对象。

答案 1 :(得分:0)

对我来说,关闭FMResultSet解决了我的问题,例如:

- (BOOL)checkKeepAccounts {
    NSString *sql = [NSString stringWithFormat:@"SELECT COUNT(*) FROM %@ where checked is 1", kGoodsListTable];
    FMResultSet *s = [self.database executeQuery:sql];
    int count = 0;
    if ([s next]) {
        count = [s intForColumnIndex:0];
    }
    **// This is the key**
    [s close];
    return count == 0;
}

不确定这是否对您有帮助,但我认为值得分享。