FMDatabaseQueue 错误:数据库被锁定 [英] FMDatabaseQueue Error: database is locked

查看:25
本文介绍了FMDatabaseQueue 错误:数据库被锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在后台线程中运行的方法,因此(据我所知)我需要使用 FMDatabaseQueue 来安全可靠地访问我的 SQLite 数据库.

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.

我正在执行查询以检查记录是否存在,然后根据结果立即UPDATEINSERT.

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:

调用 sqlite3_step 时出现未知错误(5:数据库被锁定)eu

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 查询与其他查询分开?知道为什么我的数据库在第一次查询后被锁定了吗?

Do I need to separate my SELECT query from the others somehow? Any idea why my database is locked after the first query?

推荐答案

我有同样的问题.我用全局队列创建了 sharedInstance

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

使用方法

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];

这篇关于FMDatabaseQueue 错误:数据库被锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆