iOS FMDB条件查询 select * from XXX where in array

1、数据表

CREATE TABLE map_group_ac (
  gp_id integer,
  ac_id varchar,
  PRIMARY KEY(gp_id, ac_id)
);
  1. 正确的查询语句-SQL是:
    NSMutableString *fieldString = [NSMutableString new];
    
    [fieldString appendString:@"SELECT ac_id FROM  map_group_ac where gp_id  IN ("];
 
    for (NSNumber *gpNoin groups) {
        [fieldString appendString:@"?,"];
    }
    [fieldString replaceCharactersInRange:NSMakeRange([fieldString length] -1,1)withString:@")"];
    NSString *normalString = [NSString stringWithString:fieldString];
       
     FMResultSet *ps = [shareDataBase executeQuery:normalString withArgumentsInArray:groups];   //groups is Number Array

3、(3.1-3.3)下面这几种方法试了都不起作用。3.4还未尝试

3.1

NSArray *mergeIds; // An array of NSNumber Objects  
NSString *mergeIdString = [mergeIds componentsJoinedByString:@","];  
 NSString *query = @"SELECT * FROM items WHERE last_merge_id IN (?)";   
FMResultSet *res = [self.database executeQueryWithFormat:query, mergeIdString];

3.2

 FMResultSet *ps = [shareDataBase executeQuery: @"SELECT ac_id FROM  map_group_ac  where gp_id in ?", groups];

3.3

 NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3",nil];
NSString *components = [array componentsJoinedByString:@", "];
NSMutableString *valueString = [NSMutableString new];
NSMutableString *fieldString = [NSMutableString new];[fieldString appendString:@"SELECT id FROM FriendList WHERE id NOT IN ("];
for (NSString *fielValue in array) {  
     [fieldString appendString:@"?,"];   
     [valueString appendString:fielValue];
}
[fieldString replaceCharactersInRange:NSMakeRange([fieldString length] - 1, 1) withString:@")"];
NSString *normalString = [NSString stringWithString:fieldString];
FMResultSet * rs = [self.db executeQuery:@"%@", normalString, components];

3.4
https://github.com/mehlkelm/FMDB-InOperator

本文转载自: http://blog.csdn.net/wengyupeng/article/details/60323352

你可能感兴趣的:(iOS FMDB条件查询 select * from XXX where in array)