neo4jpy2neopy2neo__0">Python操作neo4j库py2neo使用之py2neo 删除及事务相关操作(三)
py2neo 删除
1、连接数据库
python">from py2neo import Graph
graph = Graph("bolt://xx.xx.xx.xx:7687", auth=(user, pwd), name='neo4j')
2、删除节点
python"># 删除单个节点
node = graph.nodes.match(label='公司',name='XX公司').first()
graph.delete(node)# 删除多个节点
nodes = graph.nodes.match(label='公司',name='XX公司').all()
for node in nodes:graph.delete(node)
3、删除关系
python">relationship = graph.relationships.match(r_type='投资').first()
graph.delete(relationship)relationships = graph.relationships.match(r_type='投资').all()
for relationship in relationships:graph.delete(relationship)
4、 删除所有的数据和节点
python">graph.delete_all()
5、删除数据库
python">from py2neo.database import SystemGraph
system = SystemGraph("bolt://xx.xx.xx.xx:7687", auth=(user, pwd))
system.run(f'drop database {graph_name}')
py2neo 事务
python">from py2neo import Graph, Node, Relationship
from py2neo.database import Transaction
graph = Graph("bolt://xx.xx.xx.xx:7687", auth=(user, pwd), name='neo4j')# 创建事务
tn:Transaction = self.graph.begin()
# 创建节点
node1 = Node(label='公司', name='百度')
node2 = Node(label='公司', name='爱奇艺')
tn.create(node1)
tn.create(node2)
tn.graph.commit()# 创建关系
relationship = Relationship(node1,"投资",node2,**{'金额':100000})
tn.create(relationship)# 提交
tn.graph.commit()# 合并节点
tn.merge()# 删除节点、关系
tn.delete()# 回滚
tn.graph.rollback()
py2neo 其他操作
1、执行原生cyther语句
python">from py2neo import Graph
graph = Graph("bolt://100.100.20.55:7687", auth=(user, pwd), name='neo4j')# 获取label为公司,名称为XX公司的节点
graph.run("Match (n:`公司`) where n.name=`XX公司` return n")