I have a method that runs in a background thread, and so (as I understand it) I need to use FMDatabaseQueue
to safely and reliably access my SQLite database.
I'm doing a query to check for the presence of a record, after which I immediately UPDATE
or INSERT
depending on the result.
The first query runs fine and I get a count, but then the query that follows doesn't run. Here's the error I get:
Unknown error calling sqlite3_step (5: database is locked) eu
Here is my code:
//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"]];
}
}];
Do I need to separate my SELECT
query from the others somehow? Any idea why my database is locked after the first query?