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

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

pip install neo4j

        具体实现代码如下:

from neo4j import GraphDatabase

# 连接到 Neo4j 数据库
uri = "bolt://localhost:7687"  # Neo4j 数据库的URI
username = "your_username"     # Neo4j 数据库的用户名
password = "your_password"     # Neo4j 数据库的密码

# 创建一个 Neo4j 驱动程序实例
driver = GraphDatabase.driver(uri, auth=(username, password))

# 定义一个函数来执行查询
def run_query(query, parameters=None):
    with driver.session() as session:
        result = session.run(query, parameters)
        return result

# 示例查询:创建节点
create_query = "CREATE (p:Person {name: $name, age: $age})"
create_parameters = {"name": "Alice", "age": 30}
run_query(create_query, create_parameters)

# 示例查询:查询节点
match_query = "MATCH (p:Person) RETURN p.name AS name, p.age AS age"
result = run_query(match_query)

for record in result:
    print(record["name"], record["age"])

# 示例查询:更新节点
update_query = "MATCH (p:Person {name: $name}) SET p.age = $new_age"
update_parameters = {"name": "Alice", "new_age": 31}
run_query(update_query, update_parameters)

# 示例查询:删除节点
delete_query = "MATCH (p:Person {name: $name}) DELETE p"
delete_parameters = {"name": "Alice"}
run_query(delete_query, delete_parameters)

# 关闭驱动程序
driver.close()

你可能感兴趣的:(数据库,neo4j,python)