• IOS 数据库(FMDB)--- 源码介绍(FMDBResultSet)


    概述

    FMResultSet : 表示FMDatabase执行查询之后的结果集。

    基本方法

    初始化

    + (instancetype)resultSetWithStatement:(FMStatement *)statement usingParentDatabase:(FMDatabase*)aDB {
        
        FMResultSet *rs = [[FMResultSet alloc] init];
        
        [rs setStatement:statement];
        [rs setParentDB:aDB];
        
        NSParameterAssert(![statement inUse]);
        [statement setInUse:YES]; // weak reference
        
        return FMDBReturnAutoreleased(rs);
    }

    遍历

    - (BOOL)nextWithError:(NSError **)outErr {
        
        int rc = sqlite3_step([_statement statement]);
        
        if (SQLITE_BUSY == rc || SQLITE_LOCKED == rc) {
            NSLog(@"%s:%d Database busy (%@)", __FUNCTION__, __LINE__, [_parentDB databasePath]);
            NSLog(@"Database busy");
            if (outErr) {
                *outErr = [_parentDB lastError];
            }
        }
        else if (SQLITE_DONE == rc || SQLITE_ROW == rc) {
            // all is well, let's return.
        }
        else if (SQLITE_ERROR == rc) {
            NSLog(@"Error calling sqlite3_step (%d: %s) rs", rc, sqlite3_errmsg([_parentDB sqliteHandle]));
            if (outErr) {
                *outErr = [_parentDB lastError];
            }
        }
        else if (SQLITE_MISUSE == rc) {
            // uh oh.
            NSLog(@"Error calling sqlite3_step (%d: %s) rs", rc, sqlite3_errmsg([_parentDB sqliteHandle]));
            if (outErr) {
                if (_parentDB) {
                    *outErr = [_parentDB lastError];
                }
                else {
                    // If 'next' or 'nextWithError' is called after the result set is closed,
                    // we need to return the appropriate error.
                    NSDictionary* errorMessage = [NSDictionary dictionaryWithObject:@"parentDB does not exist" forKey:NSLocalizedDescriptionKey];
                    *outErr = [NSError errorWithDomain:@"FMDatabase" code:SQLITE_MISUSE userInfo:errorMessage];
                }
                
            }
        }
        else {
            // wtf?
            NSLog(@"Unknown error calling sqlite3_step (%d: %s) rs", rc, sqlite3_errmsg([_parentDB sqliteHandle]));
            if (outErr) {
                *outErr = [_parentDB lastError];
            }
        }
        
        
        if (rc != SQLITE_ROW) {
            [self close];
        }
        
        return

    列名与该列的列数的一一对应关系

    - (NSMutableDictionary *)columnNameToIndexMap {
        if (!_columnNameToIndexMap) {
            int columnCount = sqlite3_column_count([_statement statement]);
            _columnNameToIndexMap = [[NSMutableDictionary alloc] initWithCapacity:(NSUInteger)columnCount];
            int columnIdx = 0;
            for (columnIdx = 0; columnIdx < columnCount; columnIdx++) {
                [_columnNameToIndexMap setObject:[NSNumber numberWithInt:columnIdx]
                                          forKey:[[NSString stringWithUTF8String:sqlite3_column_name([_statement statement], columnIdx)] lowercaseString]];
            }
        }
        return _columnNameToIndexMap;
    }

    获取每一行中所有的结果集合

    - (NSDictionary*)resultDictionary {
        
        NSUInteger num_cols = (NSUInteger)sqlite3_data_count([_statement statement]);
        
        if (num_cols > 0) {
            NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:num_cols];
            
            int columnCount = sqlite3_column_count([_statement statement]);
            
            int columnIdx = 0;
            for (columnIdx = 0; columnIdx < columnCount; columnIdx++) {
                
                NSString *columnName = [NSString stringWithUTF8String:sqlite3_column_name([_statement statement], columnIdx)];
                id objectValue = [self objectForColumnIndex:columnIdx];
                [dict setObject:objectValue forKey:columnName];
            }
            
            return dict;
        }
        else {
            NSLog(@"Warning: There seem to be no columns in this set.");
        }
        
        return nil;
    }

    kvc的支持

    - (void)kvcMagic:(id)object {
        
        int columnCount = sqlite3_column_count([_statement statement]);
        
        int columnIdx = 0;
        for (columnIdx = 0; columnIdx < columnCount; columnIdx++) {
            
            const char *c = (const char *)sqlite3_column_text([_statement statement], columnIdx);
            
            // check for a null row
            if (c) {
                NSString *s = [NSString stringWithUTF8String:c];
                
                [object setValue:s forKey:[NSString stringWithUTF8String:sqlite3_column_name([_statement statement], columnIdx)]];
            }
        }
    }
  • 相关阅读:
    NOIP模拟题——小L的珍珠挂饰
    NOIP模拟题——小L的牛栏
    NOIP模拟题——小L的二叉树
    NOIP模拟题——愉快的logo设计
    NOIP模拟题——复制&粘贴2
    NOIP模拟题——Landscaping
    poj3264
    RMQ_ST算法
    Count Colour_poj2777(线段树+位)
    I Hate It(hdu1754)(线段树区间最大值)
  • 原文地址:https://www.cnblogs.com/guchengfengyun/p/8119924.html
Copyright © 2020-2023  润新知