Python操作neo4j库py2neo使用之py2neo 删除及事务相关操作(三)

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")
相关推荐
Scott9999HH5 小时前
【IIoT流量实战】蒸汽管道阀门全关却仍有流量?用 Python 实现涡街信号 FFT 频谱分析与温压全补偿积算网关,深度拆解靠谱的涡街流量计厂家硬核技术标准
开发语言·python
码智社6 小时前
AES加密原理详解及Java实现加解密实战
java·开发语言
AI云海6 小时前
python 列表、元组、集合和字典
开发语言·python
二十雨辰7 小时前
[爬虫]-Urllib
爬虫·python
萧瑟余晖7 小时前
JDK 26 新特性详解
java·开发语言
马优晨8 小时前
Freemarker 完整讲解(后端 Java 模板引擎)
java·开发语言·freemarker·freemarker 完整讲解·freemarker模板引擎
玉鸯8 小时前
Agent Hook:在概率推理之上,为 Agent 叠加确定性控制
python·langchain·agent
weixin_446260859 小时前
HACO:面向动态部署环境的对冲式智能计算可靠多智能体调度框架
后端·python·flask
我的xiaodoujiao9 小时前
API 接口自动化测试详细图文教程学习系列32--Allure测试报告2
python·学习·测试工具·pytest