简体   繁体   中英

Escape special characters in FMDB iOS query

I wrote a query with arguments in FMDB using ? mark symbol.I want to get details of users who has any of info in the list(hope i can give different info separated by commas in "in" statement in sql). Since my arguments having special symbols, It is throwing error. How to escape there special symbols. I tried different methods but none worked yet

My code is like:

FMResultSet *results = [db executeQuery:@"select details from user where info in(?)",infoList];
while([results next])
    {...}

Info is a string combined by different string seperated by commas.For example:

'C-note','Cuban missile crisis, the','cubbyhole','I Love Lucy','I'm a Celebrity ... Get me Out of Here!','Iacocca, Lee','-iana','Ivy League, the'

Thanks in advance

You can't use a single ? with an in clause unless you only bind a single value. It doesn't work for a list of values.

Since infoList is an array of string values, one option is to add a ? for each value in the list.

NSMutableString *query = [NSMutableString stringWithString:@"select details from user where info in ("];
for (NSInteger i = 0; i < infoList.count; i++) {
    if (i) {
        [query appendString:@","];
    }
    [query appendString:@"?"];
}
[query appendString:@")"];
FMResultSet *results = [db executeQuery:query withArgumentsInArray:infoList];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM