Swift Neo4j如何在Cypher Query中将变量放入引号中?

卢克·艾里顿

这是我的Swift Xcode项目中的代码:

@IBAction func runCypherTapped(_ sender: UIButton) {

        clientName = clientNameField.text

        guard let theo = self.theo else {
            log("Client not initialized yet")
            return
        }

        let result = theo.executeCypherSync("MATCH (c:Client) WHERE c.name = \(clientName) RETURN c.name as name, c.dob as dateOfBirth")
        switch result {
        case let .failure(error):
            log("Error while getting cypher results: \(error)")
        case let .success(queryResult):
            if let arrayClients = queryResult.rows[21]["name"] as? String {
                log("Asked via Cypher how many nodes there are with label 'name'. Answer: \(arrayClients)")

                query.append(arrayClients)
                print(query)
            } else {
                log("Got unexpected answer back")

我想将字符串变量clientName放入查询中,但在引号内,Neo4j要求在您希望检索的属性周围加上引号,例如:

MATCH (c:Client) WHERE c.name = "Mr Smith"  RETURN c.name as name

考虑到这一点,我尝试了插值并转义了字符,但没有成功。我需要上面的查询才能打印出来,但是使用“ Mr Smith”代替,而是由我的clientName String变量定义。

仅供参考,当我打印到控制台时,我得到了这个:

WHERE c.name = \"Optional(\"Mr Smith\")\" RETURN c.name as name

您可以看到它在史密斯后面加上了第一个引号,但没有第二个。

赛博

无需在Cypher查询中对引号进行硬编码。您应该只将它clientName作为字符串参数传递,这也可以使您的查询更加高效和安全:

let params: [String:PackProtocol] = ["clientName": clientName]
let result = theo.executeCypherSync(
  "MATCH (c:Client) WHERE c.name = $clientName RETURN c.name as name, c.dob as dateOfBirth",
  params: params)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章