使用值的数组Select语句

使用值的数组Select语句

本文介绍了FMDB - 使用值的数组Select语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阵列(arrayBottleID3)具有INT值的变量数量。我想回到(结果/ arrayBottleNames)返回的结果。

arrayBottleID3可能有3 INT的价值观,我不希望有做3个不同的查询语句。我不知道是什么值或arrayBottleID3多少了。

arrayBottleID3可能有1或100 INT值。

下面不起作用。我得到的错误,或它不返回任何东西。

我只是不知道确切的语法是使用withArugmentsInArray。
我知道这是不是.. bottleID =?或bottleID(?)。

有人能告诉我一个例子?

  [个体经营OpenDB]
 结果= [数据库的executeQuery:@的FriendlyName选择从库存WHERE bottleID =? withArgumentsInArray:@ [arrayBottleID3]];而([结果旁边])
{
    [arrayBottleNames ADDOBJECT:[结果stringForColumn:@友好名称]];}[数据库关闭];的NSLog(@计%D,[arrayBottleNames计数]);
的NSLog(@的名称:%@,arrayBottleNames);


解决方案

您SQL应该是:

  SELECT的FriendlyName从库存WHERE bottleID IN(?,?,?)

您将需要1 数组中的每个元素,所以你应该做这样的事情:

 的NSMutableArray *占位符= [NSMutableArray的arrayWithCapacity:[sourceArray计数]];
的for(int i = 0; I< sourceArray计数];我++){
    [占位符ADDOBJECT:@?];
}
* NSString的placeHolderString = [占位符componentsJoinedByString:@];* NSString的查询= [的NSString stringWithFormat:@SELECT * FROM MyTable的WHERE MyField的IN(%@),placeHolderString]
FMResultSet * RS = [DB的executeQuery:查询withArgumentsInArray:sourceArray];

The array (arrayBottleID3) has a variable amount of INT values. I want to return (results / arrayBottleNames) to return the results.

arrayBottleID3 may have 3 INT values, I don't want to have to do 3 different query statements. I don't know what values or how many arrayBottleID3 has.

arrayBottleID3 could have 1 or 100 INT values.

Below does not work. I get errors or it doesnt return anything.

I just dont know what the exact syntax is for using withArugmentsInArray.I know it isn't .. bottleID = ? or bottleID (?).

Can someone please show me an example?

[self OpenDB]


 results = [database executeQuery:@"SELECT friendlyname FROM Inventory WHERE bottleID = ?" withArgumentsInArray:@[arrayBottleID3]];

while([results next])
{
    [arrayBottleNames addObject:[results stringForColumn:@"friendlyname"]];

}

[database close];

NSLog(@"count %d", [arrayBottleNames count]);
NSLog(@"Names: %@",arrayBottleNames);
解决方案

Your SQL should be:

SELECT friendlyname FROM Inventory WHERE bottleID IN (?, ?, ?)

You will need 1 ? for each element in the array, so you should do something like this:

NSMutableArray *placeHolders = [NSMutableArray arrayWithCapacity:[sourceArray count]];
for (int i=0; i<[sourceArray count]; i++) {
    [placeHolders addObject:@"?"];
}
NSString *placeHolderString = [placeHolders componentsJoinedByString:@", "];

NSString *query = [NSString stringWithFormat:@"SELECT * FROM MyTable WHERE myField IN (%@)", placeHolderString];
FMResultSet *rs = [db executeQuery:query withArgumentsInArray:sourceArray];

这篇关于FMDB - 使用值的数组Select语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 01:59