數(shù)據(jù)庫語句記錄

    NSString*queryString = [NSString stringWithFormat:@"SELECT count(*) allCount FROM UserInfoTable  WHERE forMyId = %@ AND nickname like '不%%'",[WHL_SelfInfoModel sharedInstance].user_uid];
    [[self sharedInstance].userBaseQueue inTransaction:^(FMDatabase* db, BOOL* rollback) {
        FMResultSet *s = [db executeQuery:queryString];
        while ([s next]) {
            int allCount = [s intForColumn:@"allCount"];
            int totalCount = [s intForColumnIndex:0];
        }
    }];NSString*queryString = [NSString stringWithFormat:@"SELECT count(*) allCount FROM UserInfoTable  WHERE forMyId = %@ AND nickname like '不%%'",[WHL_SelfInfoModel sharedInstance].user_uid];
    [[self sharedInstance].userBaseQueue inTransaction:^(FMDatabase* db, BOOL* rollback) {
        FMResultSet *s = [db executeQuery:queryString];
        while ([s next]) {
            int allCount = [s intForColumn:@"allCount"];
            int totalCount = [s intForColumnIndex:0];
        }
    }];
allCount = totalCount = 2
    NSString*queryString = [NSString stringWithFormat:@"SELECT * FROM UserInfoTable,GroupListTable WHERE UserInfoTable.forMyId = GroupListTable.forMyId"];
UserInfoTable 表有20個字段,GroupListTable 有21個字段,查詢出來是41個

創(chuàng)建多列索引

CREATE TABLE if not exists GroupListTable g_id text not null,forMyId integer not null,CONSTRAINT uc_g_id UNIQUE (g_id,forMyId)

autoincrement 不建議使用
CONSTRAINT用意解析
SQLite INSERT OR REPLACE使用

FMDBDatabaseQueue 的內(nèi)部實現(xiàn)邏輯

- (void)inDatabase:(__attribute__((noescape)) void (^)(FMDatabase *db))block {
#ifndef NDEBUG
    /* Get the currently executing queue (which should probably be nil, but in theory could be another DB queue
     * and then check it against self to make sure we're not about to deadlock. */
    FMDatabaseQueue *currentSyncQueue = (__bridge id)dispatch_get_specific(kDispatchQueueSpecificKey);
    assert(currentSyncQueue != self && "inDatabase: was called reentrantly on the same queue, which would lead to a deadlock");
#endif
    
    FMDBRetain(self);
    
    dispatch_sync(_queue, ^() {
        
        FMDatabase *db = [self database];
        
        block(db);
        
        if ([db hasOpenResultSets]) {
            NSLog(@"Warning: there is at least one open result set around after performing [FMDatabaseQueue inDatabase:]");
            
#if defined(DEBUG) && DEBUG
            NSSet *openSetCopy = FMDBReturnAutoreleased([[db valueForKey:@"_openResultSets"] copy]);
            for (NSValue *rsInWrappedInATastyValueMeal in openSetCopy) {
                FMResultSet *rs = (FMResultSet *)[rsInWrappedInATastyValueMeal pointerValue];
                NSLog(@"query: '%@'", [rs query]);
            }
#endif
        }
    });
    
    FMDBRelease(self);
}

可以看出來其內(nèi)部維護了一個串行隊列,在當(dāng)前線程同步執(zhí)行此任務(wù).如次以來不建議在主線程執(zhí)行update、insert 等耗時量較大的任務(wù)。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容