【问题标题】:SQL Query does not fetch the required data in iphone applicationSQL Query 未在 iphone 应用程序中获取所需的数据
【发布时间】:2013-11-21 09:21:56
【问题描述】:

我在 iphone 应用程序中使用 join 从 sqlite 表中获取数据,它可以很好地获取数据,但它没有得到准确的数据,这是我的查询

select = [[NSString alloc] initWithFormat:@"select * FROM ContentMaster LEFT JOIN  Category  ON  ContentMaster.CategoryID= Category.CategoryID  LEFT JOIN Topic ON ContentMaster.TopicID=Topic.TopicID where ContentMaster.ContentTagText='%@' OR ContentMaster.ContentTitle='%@' AND Category.CategoryName='%@' AND Topic.TopicName='%@'",appDelegate.tagInput,appDelegate.tagInput,appDelegate.categoryName,appDelegate.topicName];
NSLog(@"Select is %@",select);

假设CategoryName='Product'Topic='M2M Operations'ContentTagText='Office'ContentTitle='Office' 所以当结果出现时,它会显示ContentTag='Office' 的所有记录,但它不仅仅显示ContentTag='Office' 和@ 的那些记录987654328@ 和Topic='M2M Operations'。我想可能是我缺少运营商,知道如何解决这个问题。谢谢

【问题讨论】:

    标签: ios iphone sql sqlite join


    【解决方案1】:

    据我所知,您在OR 周围缺少括号:

    SELECT * FROM ContentMaster
    LEFT JOIN  Category ON ContentMaster.CategoryID=Category.CategoryID
    LEFT JOIN Topic ON ContentMaster.TopicID=Topic.TopicID
    WHERE (ContentMaster.ContentTagText='%@' OR ContentMaster.ContentTitle='%@')
      AND Category.CategoryName='%@'
      AND Topic.TopicName='%@'
    

    【讨论】:

      【解决方案2】:

      在包含您的 sqlite 数据库的文件夹中打开终端会话并运行命令 sqlite3;在提示符处输入您的 sql 并查看您获得的结果并进行修改,直到您获得所需的结果。

      在终端中获取 sqlite 数据库文件:

      在模拟器中运行应用;

      NSLog 包含 sqlite 数据库的文件夹的路径;

      使用菜单 Go to Folder.. 将此路径复制/粘贴到 Finder 窗口;

      将文件夹从 Finder 拖到 Dock 中终端图标的顶部,或从 Applications 和 cd 打开终端应用程序到此路径;

      【讨论】:

      • sqlite 数据库有数据存储在应用程序中,我将如何在终端窗口中检查它
      • 答案已编辑以包括如何获取包含数据库的文件夹
      • 使用firefox sqlitemanager插件你会得到数据~library-application support-simulator-your application-your.db
      猜你喜欢
      • 1970-01-01
      • 2017-03-14
      • 1970-01-01
      • 2012-03-06
      • 2010-12-31
      • 2010-09-08
      • 1970-01-01
      • 1970-01-01
      • 2011-07-06
      相关资源
      最近更新 更多