Skip to main content
 首页 » 编程设计

python之Neo4j:为什么我不能在 python 中通过 find_one 找到特定节点

2025年02月15日7fff_TT

我刚开始在 python2.7 中使用 Neo4j。现在我尝试了一个关于 Neo4j 的简单测试,但我无法在数据库中找到特定的节点。节点存在,但返回结果为'None'。 这是我的代码:

from py2neo import Graph,Node,Relationship 
test_graph = Graph( 
    "http://localhost:7474",  
    username="neo4j",  
    password="******" 
) 
test_node_1 = Node(label = "Person",name = "test_node_1") 
test_node_2 = Node(label = "Person",name = "test_node_2") 
test_graph.create(test_node_1) 
test_graph.create(test_node_2) 
 
node_1_call_node_2 = Relationship(test_node_1,'CALL',test_node_2) 
node_2_call_node_1 = Relationship(test_node_2,'CALL',test_node_1) 
test_graph.create(node_1_call_node_2) 
test_graph.create(node_2_call_node_1) 
 
find_code_1 = test_graph.find_one( 
  label="Person", 
  property_key="name", 
  property_value="test_node_1" 
) 
print (find_code_1) 

请您参考如下方法:

问题是您的节点语法略有偏差。您正在设置一个名为 label 的属性,其值为 Person,您并不是在创建 Person 节点。

语法应该是这样的:

test_node_1 = Node("Person",name = "test_node_1") 
test_node_2 = Node("Person",name = "test_node_2") 

希望这对您有所帮助。

问候, 汤姆