FMDatabaseQueue Error: database is locked

2019-05-10 12:08发布

问题:

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?

回答1:

I have the same issue. I made sharedInstance with global queue

context.h

@interface context : NSObject
{
    FMDatabaseQueue *_queue;
}

+ (context *)sharedInstance;
@property(strong, nonatomic, readwrite) FMDatabaseQueue *queue;
@end

context.m

#import "context.h"

@implementation context

@synthesize queue = _queue;

+ (context *)sharedInstance {
    static dispatch_once_t onceToken;
    static context *instance = nil;
    dispatch_once(&onceToken, ^{
        instance = [[context alloc] init];
    });
    return instance; 
}

- (id)init {
    self = [super init];
    if (self) {
        _queue          = [FMDatabaseQueue databaseQueueWithPath:YOUR_SQLITE_FILE_PATH];

    }
    return self; 
}

@end

How to use it

context *appContext = [context sharedInstance];

[appContext.queue inDatabase:^(FMDatabase *db) {
FMResultSet *results = [db executeQuery:@"SELECT * FROM something"];

if([results next]) {
    NSLog(@"results dump = %@", [results resultDictionary]);
}
[results close];