【问题标题】:Sqlite IN Clause with NSArray带有 NSArray 的 Sqlite IN 子句
【发布时间】:2012-11-30 21:23:27
【问题描述】:

我需要在 Objective-C 中使用带有 NSArray 的 IN 子句来执行 Select 语句。像这样的

NSArray *countries = [NSArray arrayWithObjects:@"USA", @"Argentina", @"England", nil];

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

    const char *sqlStatement =
    [[NSString stringWithFormat:@"select currencies from money where country IN countries] UTF8String];

....

其中 IN countries 是带有字符串值的 NSArray。这可能吗?

【问题讨论】:

  • 请花点时间重新访问您以前的questions 并在适用的情况下接受一些答案。您只需单击您找到的最佳答案左侧的大复选框。这是在 StackOverflow 上做的礼貌的事情。请参阅FAQ 了解更多信息。

标签: objective-c sqlite nsarray


【解决方案1】:

将查询字符串更改为:

const char *sqlStatement =
[NSString stringWithFormat:@"select currencies from money where country IN ('%@')",[countries componentsJoinedByString:@"','"]] UTF8String];

【讨论】:

  • 非常感谢!,差不多了。当我输出查询时,我在不同的行中看到数组值,没有返回结果: select currency from money where country IN('USA','Argent','England') 当我在 sqlite 客户端上进行相同的查询时它返回没有结果,但是当我在同一行中进行相同的查询时,查询工作正常,返回正确的结果。有没有办法 componentsJoinedByString 在同一行输出字符串?再次感谢!
  • @user1424334 试试这个:[NSString stringWithFormat:@"select currency from money where country IN ('%@')",[countries componentsJoinedByString:@"','"]]
  • @user1424334 你试过这个吗??
  • 这才是做出select语句的正确方式。然而,在此之前,我必须从字符串中删除空格和换行符。
猜你喜欢
  • 2016-03-25
  • 1970-01-01
  • 1970-01-01
  • 2021-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-09
相关资源
最近更新 更多