iOS FMDB 通过动态表名和动态字段查询数据

通过动态表名和动态字段查询数据,一般我们会这样写:

NSString *sql = [NSString stringWithFormat:@"SELECT * FROM %@ WHERE obj = %@", 表名,字段内容];  

但是这样写FMDB会报错

正确写法:

- (BOOL)seekString:(NSString *) value WithTabel:(NSString *) tableName {
    
    
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    
    NSString *databaseFilePath = [cachePath stringByAppendingPathComponent:kSqliteName];
    
    FMDatabase *database = [[FMDatabase alloc]initWithPath:databaseFilePath];
    
    if ([database open]) {
       
        NSString *sql1 = @"SELECT page FROM ";
        // obj 字段名
        NSString *sql2 = @"WHERE obj = ?";
        // tableName 表名
        NSString *sql = [sql1 stringByAppendingString:tableName];
        sql = [sql stringByAppendingString:sql2];
        // value  字段内容
        FMResultSet *result = [database executeQuery:sql, value];
        while ([result next]) {
            return YES;
        }
    }
    
    [database close];
    
    return NO;
}

你可能感兴趣的:(iOS FMDB 通过动态表名和动态字段查询数据)