大家好,我对python中的neo4j( new 4j-driver)非常陌生。当检查节点是否不存在时,我遇到了一个问题,我通过这些代码发送了一些与db中的节点不匹配的名称。
from neo4j import GraphDatabase
driver = GraphDatabase.driver('bolt://localhost:7687', auth=(user, pass))
session = driver.session()
def matchNode(name):
Label = 'SINGLE_NODE'
return session.run("MATCH (a:"+Label+") WHERE a.name= $name "
"RETURN id(a)", name=name).single().value()
name = 'test'
nodeID = matchNode(name)
if nodeID:
print("Exist")
else:
print("Not Exist")但它的错误如下所示,因为它在数据库中没有任何匹配的节点。
Traceback (most recent call last):
File ".\neo4jdriver.py", line 44, in <module>
props = matchNode(driver,'test')
File ".\neo4jdriver.py", line 25, in matchNode
"RETURN id(a)", name=name).single().value()
AttributeError: 'NoneType' object has no attribute 'value'
Failed to write data to connection Address(host='localhost', port=7687) (Address(host='127.0.0.1', port=7687)); ("0; 'Underlying socket connection gone (_ssl.c:2263)'")
Failed to write data to connection Address(host='localhost', port=7687) (Address(host='127.0.0.1', port=7687)); ("0; 'Underlying socket connection gone (_ssl.c:2263)'")
Failed to write data to connection Address(host='localhost', port=7687) (Address(host='127.0.0.1', port=7687)); ("0; 'Underlying socket connection gone (_ssl.c:2263)'")
Failed to write data to connection Address(host='localhost', port=7687) (Address(host='127.0.0.1', port=7687)); ("0; 'Underlying socket connection gone (_ssl.c:2263)'")
Failed to write data to connection Address(host='localhost', port=7687) (Address(host='127.0.0.1', port=7687)); ("0; 'Underlying socket connection gone (_ssl.c:2263)'")那么,如果节点不存在,我该如何解决这个问题并返回none呢?谢谢
发布于 2019-08-04 04:39:39
在matchNode函数中,执行一个try /except块,其中try返回session.run,except返回None
https://stackoverflow.com/questions/57341224
复制相似问题