我想检查neo4j中是否已存在节点。我使用以下代码:
from py2neo import Graph
from py2neo import *
authenticate("localhost:7474","neo4j", "somepassword")
graph = Graph("http://localhost:7474/db/data/")
graph.cypher.execute("CREATE(alice:Person {name:'Alice' , age:15})->[:Friendof]->(bob:Person {name: 'Bob' , age:14})")
for record in graph.cypher.execute("MATCH(p {title:'The Movie'})RETURN p"):
if (record[0]==None):
print "None"
else:
print (record[0])
我没有一部名为“The Movie'”的电影,但我没有#34;没有"打印。还有其他方法可以做到这一点吗?
答案 0 :(得分:2)
“无”不打印,因为没有记录要迭代,所以不执行print语句。
相反,您可以检查是否有任何记录返回:
results = graph.cypher.execute("MATCH (p:Movie {title:'The Movie'}) RETURN p")
if results:
for record in results:
# do something with each record...
else:
print "None"