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];
}
}];
NSString*queryString = [NSString stringWithFormat:@"SELECT * FROM UserInfoTable,GroupListTable WHERE UserInfoTable.forMyId = GroupListTable.forMyId"];
创建多列索引
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 的内部实现逻辑
- (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);
}
可以看出来其内部维护了一个串行队列,在当前线程同步执行此任务.如次以来不建议在主线程执行update、insert 等耗时量较大的任务。