【问题标题】:(Python,Neo4j-driver) - How return none when check if node does not exist(Python,Neo4j-driver) - 检查节点是否不存在时如何返回none
【发布时间】:2019-08-03 18:46:05
【问题描述】:

你好,我是 python 中的 neo4j(neo4j-driver) 的新手。检查节点是否不存在时出现问题,我通过这些代码发送了一些与数据库中的节点不匹配的名称。

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")

但它会出现这样的错误,因为它没有与 db 中的节点匹配。

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)'")

那么如果节点不存在,我该如何解决这个问题并且不返回任何内容。谢谢

【问题讨论】:

    标签: python neo4j neo4j-bolt neo4j-driver


    【解决方案1】:

    在你的 matchNode 函数中,你执行一个 try /except 块,其中 try 返回 session.run 并且 except 返回 None

    【讨论】:

      猜你喜欢
      • 2014-04-03
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-03
      • 1970-01-01
      相关资源
      最近更新 更多