当前位置:   article > 正文

python3实现对neo4j图数据库的增删改查操作_python 连接neo4j

python 连接neo4j

        可以使用 neo4j Python 驱动程序来执行对 Neo4j 数据库的增删改查操作。首先,确保已经安装了 neo4j Python 驱动程序。

pip install neo4j

        具体实现代码如下:

  1. from neo4j import GraphDatabase
  2. # 连接到 Neo4j 数据库
  3. uri = "bolt://localhost:7687" # Neo4j 数据库的URI
  4. username = "your_username" # Neo4j 数据库的用户名
  5. password = "your_password" # Neo4j 数据库的密码
  6. # 创建一个 Neo4j 驱动程序实例
  7. driver = GraphDatabase.driver(uri, auth=(username, password))
  8. # 定义一个函数来执行查询
  9. def run_query(query, parameters=None):
  10. with driver.session() as session:
  11. result = session.run(query, parameters)
  12. return result
  13. # 示例查询:创建节点
  14. create_query = "CREATE (p:Person {name: $name, age: $age})"
  15. create_parameters = {"name": "Alice", "age": 30}
  16. run_query(create_query, create_parameters)
  17. # 示例查询:查询节点
  18. match_query = "MATCH (p:Person) RETURN p.name AS name, p.age AS age"
  19. result = run_query(match_query)
  20. for record in result:
  21. print(record["name"], record["age"])
  22. # 示例查询:更新节点
  23. update_query = "MATCH (p:Person {name: $name}) SET p.age = $new_age"
  24. update_parameters = {"name": "Alice", "new_age": 31}
  25. run_query(update_query, update_parameters)
  26. # 示例查询:删除节点
  27. delete_query = "MATCH (p:Person {name: $name}) DELETE p"
  28. delete_parameters = {"name": "Alice"}
  29. run_query(delete_query, delete_parameters)
  30. # 关闭驱动程序
  31. driver.close()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/947567
推荐阅读
相关标签
  

闽ICP备14008679号