gpt4 book ai didi

ios - FMDB:更新时数据库被锁定

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:08:19 27 4
gpt4 key购买 nike

我正在使用 FMDB,当我想返回一些值时我正在使用 FMDatabase,当我不需要时我正在使用 FMDatabaseQueue返回任何值。当我添加到数据库并使用 FMDatabaseQueue 我没有错误(除非我在开​​始时创建一个新数据库,当我删除数据库然后创建 [no database to delete 所以它创建一个新的])。问题是当我使用相同的 FMDatabaseQueue 更新我的 database 时出现错误:

Unknown error finalizing or resetting statement (5: database is locked)

DB Query: UPDATE traps_table SET dist=34721.853417, dist_to_close_point=35734.768293 WHERE _id=10

当我尝试对数据库文件本身进行查询时,我没有发现任何错误,一切都很顺利。

代码如下:

- (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]]);
}

函数引用的代码:

- (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]);
}
}

我的代码有什么问题吗?

编辑:

TrapService.mm:

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

#define trapsDB ([SQLiteHandler sharedDatabase])

SQLiteHandler.m:

@property (strong, nonatomic) FMDatabase *db;

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

EDIT-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 注入(inject)问题和引用问题 NSString 变量),所以我也调整了上面的 SQL。此外,如果 SQL 语句失败,我总是记录 lastErrorMessage,否则我只是在猜测 SQL 失败的原因。使用或者在您认为合适的时候忽略这些不相关的调整。)

但要点是当已经在 inDatabase 中时,永远不要调用 inDatabase。不过,奇怪的是,在 inDatabase 中调用 inDatabase 通常会导致死锁,而不是“数据库已锁定”错误。坦率地说,FMDatabaseQueue 的全部目的是协调来自不同线程的数据库请求,使数据库永远不会繁忙。您通常会收到“数据库已锁定”错误的唯一情况是您有多个 FMDatabaseQueueFMDatabase 对象。您的应用程序在任何给定时间都应该只打开一个数据库对象。

关于ios - FMDB:更新时数据库被锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21215526/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com