我有一个在后台线程中运行的方法,因此(据我所知)我需要使用FMDatabaseQueue
来安全可靠地访问我的SQLite数据库。
我正在进行查询以检查记录是否存在,之后根据结果立即进行UPDATE
或INSERT
操作。
第一个查询正常运行并返回计数,但随后的查询无法运行。这是我收到的错误:
Unknown error calling sqlite3_step (5: database is locked) eu
以下是我的代码:
//Establish database queue
NSString *path = [[PPHelpers documentsPath] stringByAppendingPathComponent:@"PilotPro2.db"];
FMDatabaseQueue *queue = [FMDatabaseQueue databaseQueueWithPath:path];
//Start thread-safe database queue
[queue inDatabase:^(FMDatabase *dbq) {
NSUInteger count;
//The other parameters in this query are provided beforehand
NSString *query = [NSString stringWithFormat:@"SELECT COUNT(%@) AS counter FROM %@ WHERE %@ = '%@'",columnID, model, columnID, dict[columnID]];
FMResultSet *countResult = [dbq executeQuery:query]; //This works fine
while([countResult next]) {
count = [countResult intForColumn:@"counter"];
}
[countResult close];
if(count > 0){
//--- UPDATE
//-- This is where FMDB throws the error...
[dbq executeUpdate:[PPDatabase editAircraftQuery:dict[columnID]], dict[@"aircraftRegistration"], dict[@"makeModel"], dict[@"categoryClass"], dict[@"highPerformance"], dict[@"complex"], dict[@"turbine"], dict[@"turboprop"], dict[@"tailwheel"], dict[@"active"]];
}else{
//--- INSERT
[dbq executeUpdate:[PPDatabase addAircraftQuery], dict[@"aircraftID"], dict[@"aircraftRegistration"], dict[@"makeModel"], dict[@"categoryClass"], dict[@"highPerformance"], dict[@"complex"], dict[@"turbine"], dict[@"turboprop"], dict[@"tailwheel"], dict[@"active"]];
}
}];
我需要将我的 SELECT
查询与其他查询分开吗?有什么想法为什么第一个查询后我的数据库被锁定了吗?
FMDatabaseQueue
实例。同时确保不会有任何未使用的FMDatabase
实例存在。 - Rob