美文网首页
iOS FMDB 通过动态表名和动态字段查询数据

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

作者: 不知蜕变的挣扎 | 来源:发表于2017-03-20 09:54 被阅读213次

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

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 通过动态表名和动态字段查询数据

      本文链接:https://www.haomeiwen.com/subject/zvjbnttx.html