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ù)。