python连接neo4j
2026-11-29 03:48:13

Python连接Neo4j(图片来源网络,连接(jie)侵删)

安装neo4j驱动

python连接neo4j

需要安装Python的连接Neo4j驱动,可以使用pip进行安装(zhuang):

python连接neo4j

pip install neo4j

连接到Neo4j数据库

python连接neo4j

接下来,连接我们需要使用Python连接到Neo4j数据库,连接(jie)以下是连接一个简单的(de)示例:

from neo4j import GraphDatabaseclass Neo4jConnection: def __init__(self, uri, user, password): self._driver = GraphDatabase.driver(uri, auth=(user, password)) def close(self): self._driver.close() def run_query(self, query): with self._driver.session() as session: result = session.run(query) return result替换为你的Neo4j数据库URI、用(yong)户名和密码uri = "bolt://localhost:7687"user = "neo4j"password = "your_password"connection = Neo4jConnection(uri,连接 user, password)

执行Cypher查询

现在(zai)我们已经连接(jie)到Neo4j数据库,我们可以执行Cypher查询了,连接以下是连接一个简单的示例:

query = "MATCH (n) RETURN n LIMIT 5"result = connection.run_query(query)for record in result: print(record["n"])connection.close()

这个示例将返回数据库中前5个节点,你可以根据(ju)需要修(xiu)改查询语句。连接

连接

(作者:整站优化)