【问题标题】:How to perform SQL string contains query in Azure DB with iOS Swift?如何使用 iOS Swift 在 Azure DB 中执行 SQL 字符串包含查询?
【发布时间】:2019-11-11 01:59:14
【问题描述】:

我正在尝试使用 iOS swift 在 Azure DB 中执行包含查询的字符串。

但是,我收到以下错误: 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“无法解析格式字符串“CONTAINS(title,"panda")"'

以下是我执行 Azure SQL 查询的代码:

static func tableQueryOrderBy(_ tableName: String, queryCondition: String,
                                  orderBy: String, isDescending: Bool,
                                  fetchOffset: Int, fetchLimit: Int,
                                  queryCallBack: @escaping ([AnyObject]?) -> ()) {

        guard let delegate = UIApplication.shared.delegate as? AppDelegate,
            let client = delegate.client else { return }

        // condition
        guard let table = client.table(withName: tableName) else { return }
        guard let query = MSQuery(table: table) else { return }
        if queryCondition != "" {
            query.predicate = NSPredicate(format: queryCondition)
        }
        isDescending ? query.order(byDescending: orderBy) : query.order(byAscending: orderBy)
        query.fetchOffset = fetchOffset
        query.fetchLimit = fetchLimit
        query.read { result, error in
            if let err = error {
                // print("Query failed: ", err.localizedDescription)
                queryCallBack(nil)

            } else if let items = result?.items {
                // print("Query succeeded!")
                queryCallBack(items as [AnyObject]?)
            }
        }
    }

【问题讨论】:

    标签: ios sql swift azure


    【解决方案1】:

    终于找到了@Sanket Ray 的示例,将他/她的答案复制粘贴在这里:

    let firstName = NSPredicate(format: "firstName CONTAINS[c] %@", searchText)
    let lastName = NSPredicate(format: "lastName CONTAINS[c] %@", searchText)
    
    let orCompoundPredicate = NSCompoundPredicate(orPredicateWithSubpredicates: 
    [firstNamePredicate,lastNamePredicate]
    

    参考:NSPredicate: Combine CONTAINS with IN

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-20
      相关资源
      最近更新 更多