ios FMDB SELECT NOT IN(?)

时间:2014-11-14 07:59:01

标签: ios select fmdb

我想检查一下id列表是否在数据库中。 这是我的代码......

NSArray *array = [NSArray arrayWithObjects:@"1",@"2",@"3",nil];
NSString *string = [array componentsJoinedByString:@","];
FMResultSet * rs = [self.db executeQuery:@"SELECT id FROM FriendList WHERE id NOT IN (?)",string];

这不起作用。如果数组只有一个数字,它才有效。

任何人都知道如何使用FMDB进行SELECT NOT IN?

谢谢!

1 个答案:

答案 0 :(得分:1)

这里你几乎不需要修改。

按照以下代码。

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];
fieldString = nil;

FMResultSet * rs = [self.db executeQuery:@"%@", normalString, components];

修改

根据您在评论中提供给我的信息,我确实发现当您从数组组件中提供多个项目时,sqlite无法编译字符串文字。我还在修改答案,以测试它是否正常工作?发表评论以确认。