python中使用neo4j

参考上片问文档,除了在控制台输入查询外,还可在python中执行查询命令

1.find单个节点。查询tom信息

bash 复制代码
from py2neo import Graph

graph = Graph('bolt://xx.xx.xx.xx:7687',auth=("neo4j","neo4j1234"))
print ("数据库连接正常")

result = graph.run("match (tom {name:'Tom Hanks'}) return tom")

for record in result:
    person_node = record["tom"]
    print(person_node)

2.以节点中的某个属性去查询

修改了下脚本,将url等登录信息放进配置文件中。

bash 复制代码
import configparser
from py2neo import Graph

config = configparser.ConfigParser()
config.read('config.ini')

#获取数据库连接
neo4j_config = config['neo4j']
neo4j_url = neo4j_config['neo4jUrl']
neo4j_name = neo4j_config['neo4jName']
neo4j_passwd = neo4j_config['neo4jPasswd']


graph = Graph(neo4j_url,auth=(neo4j_name,neo4j_passwd))
print ("数据库连接正常")

result = graph.run("match (cloudAtlas {title:'Cloud Atlas'}) return cloudAtlas").data()

for record in result:
    person_node = record["cloudAtlas"]
    print(person_node)

3.查询指定个数的信息

python 复制代码
import configparser
from py2neo import Graph

config = configparser.ConfigParser()
config.read('config.ini')

#获取数据库连接
neo4j_config = config['neo4j']
neo4j_url = neo4j_config['neo4jUrl']
neo4j_name = neo4j_config['neo4jName']
neo4j_passwd = neo4j_config['neo4jPasswd']


graph = Graph(neo4j_url,auth=(neo4j_name,neo4j_passwd))
print ("数据库连接正常")

result = graph.run("match (cloudAtlas:Person) return cloudAtlas limit 10")

for record in result:
    person_node = record["cloudAtlas"]
    print(person_node)

4.条件查询

python 复制代码
import configparser
from py2neo import Graph

config = configparser.ConfigParser()
config.read('config.ini')

#获取数据库连接
neo4j_config = config['neo4j']
neo4j_url = neo4j_config['neo4jUrl']
neo4j_name = neo4j_config['neo4jName']
neo4j_passwd = neo4j_config['neo4jPasswd']


graph = Graph(neo4j_url,auth=(neo4j_name,neo4j_passwd))
print ("数据库连接正常")

result = graph.run("match (nineties:Movie) where nineties.released >= 1990 and nineties.released < 2000 return nineties.title limit 5")

for record in result:
    person_node = record["nineties.title"]
    print(person_node)

5.查询出tom出演的电影名称

python 复制代码
import configparser
from py2neo import Graph

config = configparser.ConfigParser()
config.read('config.ini')

#获取数据库连接
neo4j_config = config['neo4j']
neo4j_url = neo4j_config['neo4jUrl']
neo4j_name = neo4j_config['neo4jName']
neo4j_passwd = neo4j_config['neo4jPasswd']


graph = Graph(neo4j_url,auth=(neo4j_name,neo4j_passwd))
print ("数据库连接正常")
query="match (tom:Person{ name:'Tom Hanks'})-[:ACTED_IN]->(tomHanksMovies) return tom, tomHanksMovies"
result = graph.run(query)

for record in result:
    name = record["tom"]
    movies = record["tomHanksMovies"]
    print(name, movies)

后续的操作,只需要在query中填写需要的查询命令,就能操作各种查询。

相关推荐
lubiii_1 分钟前
MCP应用:cursor+hexstrike-ai的安全实战
开发语言·web安全·ai·php
是罐装可乐5 分钟前
前端架构知识体系:深入理解 sessionStorage、opener 与浏览器会话模型
开发语言·前端·javascript·promise·语法糖
cd ~/Homestead6 分钟前
PHP 变量、类型、运算符
android·开发语言·php
何中应8 分钟前
在Coze上新建一个插件
开发语言·python·ai
rgeshfgreh14 分钟前
Conda降级Python版本全指南
python
前端程序猿之路14 分钟前
30天大模型学习之Day3:高级 Prompt 工程
人工智能·python·学习·语言模型·大模型·prompt·ai编程
郝学胜-神的一滴16 分钟前
深入理解Qt中的坐标系统:鼠标、窗口与控件位置详解
开发语言·c++·qt·程序人生
也许是_17 分钟前
大模型应用技术之 Agent框架 AutoGPT
人工智能·python
福楠17 分钟前
C++ | 继承
c语言·开发语言·数据结构·c++·算法
The star"'18 分钟前
Deepseek基础,模板引擎,prompt提示词,增强检索,智能机器人
python·机器人·云计算·prompt·easyui