FMDatabaseQueue error: database is locked

I have a method that runs on a background thread and so (as I understand it) I need to use FMDatabaseQueue

to access my SQLite database safely and reliably.

I make a request to check if a record exists, after which I immediately UPDATE

or INSERT

depending on the result.

The first request runs fine and I get an invoice, but then the next request fails. Here is the error I am getting:

Unknown error when calling sqlite3_step (5: database 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 request SELECT

from others in some way ? Any idea why my database is locked after the first request?

+3


source to share


1 answer


I have the same problem. I did sharedInstance with a 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];

      

+3


source







All Articles