gpt4 book ai didi

swift - 如何使用 Theo 和 Neo4j 数据库从 Swift 检索节点属性?

转载 作者:行者123 更新时间:2023-11-28 07:55:27 24 4
gpt4 key购买 nike

下面是我的 Swift Xcode 项目中的代码片段。我目前正在使用 Swift 在 Xcode 中开发一个应用程序,我正在尝试使用 Neo4j 作为存储所有数据等的后端。

当谈到使用 Swift 和 Neo4j 时,唯一的选择是使用 Theo 来连接 Neo4j 数据库并与之通信。您可以直接执行 Cypher 查询,尽管当我想从 Neo4j 数据库中获取数据并将其存储在 Swift 中的本地变量或数组中时,这已被证明是相当困难的。

我的代码,请记住,这是直接取自 Neo4j Theo Swift 示例包,您可以在此处找到 - https://github.com/GraphStory/neo4j-ios

import UIKit
import Theo
import PackStream

class QueryViewController: UIViewController {

var connectionConfig: ConnectionConfig?
@IBOutlet weak var outputTextView: UITextView?

//All of the User Interface Outlets, Buttons etc.
@IBOutlet weak var createNodeButton: UIButton?
@IBOutlet weak var fetchNodeButton: UIButton?
@IBOutlet weak var runCypherButton: UIButton?
@IBOutlet weak var runTransactionButton: UIButton?
@IBOutlet weak var clientNameField: UITextField!
@IBOutlet weak var clientYearBorn: UITextField!
@IBOutlet weak var nodeInfo: UILabel!

var query: String!
var clientArray: [String] = []

var clientID : Int = 0
var clientName: String!
var yearBorn: String!

private var theo: BoltClient?
private var lastNodeId: UInt64 = 1

override func viewDidLoad() {
super.viewDidLoad()

disableButtons()
if let config = connectionConfig {
do {
self.theo = try BoltClient(
hostname: config.host,
port: config.port,
username: config.username,
password: config.password,
encrypted: true)
} catch {
DispatchQueue.main.async { [weak self] in
self?.outputTextView?.text = "Failed during connection configuration"
}
return
}

guard let theo = self.theo else { return }

log("Connecting...")

let result = theo.connectSync()
switch result {
case .failure(_):
log("Error while connecting")
case .success(_):
let result = theo.executeCypherSync("MATCH (n:ImpossibleNode) RETURN count(n) AS n")
switch result {
case let .failure(error):
log("Error while connecting: \(error)")
case .success(_):
log("Connected")
DispatchQueue.main.async { [weak self] in
self?.enableButtons()
}
}
}

} else {
outputTextView?.text = "Missing connection configuration"
}
}

private func enableButtons() {
createNodeButton?.isEnabled = true
fetchNodeButton?.isEnabled = true
runCypherButton?.isEnabled = true
runTransactionButton?.isEnabled = true
}

private func disableButtons() {
createNodeButton?.isEnabled = false
fetchNodeButton?.isEnabled = false
runCypherButton?.isEnabled = false
runTransactionButton?.isEnabled = false
}

//Here we can create a Node to add to the Neo4j Database.
@IBAction func createNodeTapped(_ sender: UIButton) {

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

//Here we define some variables we wish to store within the node as Properties.
clientName = clientNameField?.text
yearBorn = clientYearBorn.text
clientID = Int(lastNodeId)

self.lastNodeId += 1

let node = Node(label: "Client", properties:["id": clientID, "name": clientName, "yearBorn": yearBorn])

let result = theo.createAndReturnNodeSync(node: node)
switch result {
case let .failure(error):
log("Error while creating node: \(error)")
case let .success(responseNode):
log("Successfully created node: \(responseNode)")
lastNodeId = responseNode.id!

clientName = clientNameField?.text
print("Added", clientName!, "to the Nexus")
print(clientID)

}
}

@IBAction func fetchNodeTapped(_ sender: UIButton) {

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

theo.nodeBy(id: lastNodeId) { result in
switch result {
case let .failure(error):
self.log("Error while reading fetched node with ID '\(self.lastNodeId)': \(error)")
case let .success(responseNode):
if let responseNode = responseNode {
self.log("Fetched node with ID \(self.lastNodeId): \(responseNode)")

//Here I am attempting to grab all the nodes labeled "Client" with the property name "Mr Davies"

let results = theo.executeCypherSync("MATCH (c:Client{name: 'Mr Davies'}) RETURN (c)")

//Here I am appending the results from the Cypher Query above to an array - clientArray
self.clientArray.append((results.value?.nodes.description)!)

//Finally here I am printing to the console the client array.
print(self.clientArray)
} else {
self.log("Could not find node with ID \(self.lastNodeId)")
}
}
}
}

func log(_ string: String) {
print(string)
DispatchQueue.main.async {
let text = self.outputTextView?.text ?? ""
if text == "" {
self.outputTextView?.text = string
} else {
self.outputTextView?.text = "\(string)\n\n\(text)"

}
}
}

@IBAction func runCypherTapped(_ sender: UIButton) {

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

let result = theo.executeCypherSync("MATCH (c:Client) RETURN count(c) AS num")
switch result {
case let .failure(error):
log("Error while getting cypher results: \(error)")
case let .success(queryResult):
if let intNum = queryResult.rows[0]["num"] as? UInt64 {
log("Asked via Cypher how many nodes there are with label Client. Answer: \(intNum)")

} else {
log("Got unexpected answer back")

}
}
}

@IBAction func runTransactionTapped(_ sender: UIButton) {

do {
try theo?.executeAsTransaction(transactionBlock: { (tx) in
let query = "CREATE (C:Client { name: {name} } )"
self.theo?.executeCypherSync(query, params: ["prop": "name"])
self.theo?.executeCypherSync(query, params: ["prop": "yearborn"])

})
} catch {
log("Error while executing transaction: \(error)")
return
}

log("Transaction completed successfully")
}
}

我对这个应用程序的目标是获取所有标记为“客户端”的节点,然后将所述节点的属性存储到 Swift 中的 1 个或多个本地数组中。然后我可以着手处理数据,在“fetchNodeTapped”函数中我已经注释了代码。您可以看到我将数组内容打印到控制台,这是我得到的:

Fetched node with ID 260: Theo.Node ["[244: Theo.Node, 229: Theo.Node, 154: Theo.Node, 230: Theo.Node, 215: Theo.Node, 226: Theo.Node, 249: Theo.Node, 177: Theo.Node, 277: Theo.Node, 214: Theo.Node, 278: Theo.Node, 222: Theo.Node, 285: Theo.Node, 206: Theo.Node, 158: Theo.Node, 258: Theo.Node, 217: Theo.Node, 167: Theo.Node, 176: Theo.Node, 223: Theo.Node, 153: Theo.Node, 168: Theo.Node, 266: Theo.Node, 228: Theo.Node, 169: Theo.Node, 220: Theo.Node, 247: Theo.Node, 150: Theo.Node, 284: Theo.Node, 216: Theo.Node, 156: Theo.Node, 272: Theo.Node, 243: Theo.Node, 255: Theo.Node, 264: Theo.Node, 268: Theo.Node, 205: Theo.Node, 273: Theo.Node, 320: Theo.Node, 175: Theo.Node, 151: Theo.Node, 257: Theo.Node, 225: Theo.Node, 172: Theo.Node, 270: Theo.Node, 274: Theo.Node, 246: Theo.Node, 224: Theo.Node, 245: Theo.Node, 283: Theo.Node, 248: Theo.Node, 170: Theo.Node, 318: Theo.Node, 160: Theo.Node, 218: Theo.Node, 152: Theo.Node, 184: Theo.Node, 256: Theo.Node, 260: Theo.Node, 231: Theo.Node, 173: Theo.Node, 171: Theo.Node, 149: Theo.Node, 157: Theo.Node, 161: Theo.Node, 221: Theo.Node, 271: Theo.Node, 227: Theo.Node, 275: Theo.Node, 276: Theo.Node, 279: Theo.Node, 155: Theo.Node, 183: Theo.Node, 219: Theo.Node, 265: Theo.Node, 267: Theo.Node, 162: Theo.Node, 250: Theo.Node, 263: Theo.Node, 269: Theo.Node, 319: Theo.Node, 159: Theo.Node]"]

在这里您可以看到我的函数正在存储每个“Mr Davies”节点,当我将它打印到控制台时,它会打印出每个节点 ID“Theo.Node, 319: 例如。我想访问的特定属性每个节点,这样我就可以从数据库中检索所有属性(姓名、出生年份等),然后如上所述将这些属性存储在本地。

如果有人可以提供帮助,我们将不胜感激,如果您对我的问题有任何进一步的疑问,请开火。

最佳答案

我不知道我是否完全理解你的问题,但你可以使用 keys()函数返回节点或关系的属性名称。例如:

创建示例节点:

CREATE (:Node {firstname : "Jon", lastname : "Doe", age : 20})

然后:

MATCH (node:Node)
RETURN keys(node)

输出将是:

╒══════════════════════════════╕
│"keys(node)" │
╞══════════════════════════════╡
│["firstname","age","lastname"]│
└──────────────────────────────┘

关于swift - 如何使用 Theo 和 Neo4j 数据库从 Swift 检索节点属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48228896/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com