简体   繁体   English

如何使用 python 获取 neo4j 中的所有数据库名称?

[英]how to get all database names in neo4j using python?

from neo4j import GraphDatabase

driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password"))

with driver.session() as session:
        result = session.run("SHOW DATABASES")
        print(result)

the result is <neo4j.work.result.Result object at 0x0000028F2BB30208>结果是 <neo4j.work.result.Result object at 0x0000028F2BB30208>

How to get database names?如何获取数据库名称?

Add this code, this works for me添加此代码,这对我有用

 databasesNames = list()
 queryDatabases="SHOW DATABASES;" 
 try:
     driver=GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password"))
     session=driver.session(database='system')
     result = session.run(queryDatabases)
     for record in result:
         databasesNames.append(record["name"])
     exit
  except Exception as e:
        print("Connection to neo4j failed: ", e)
  print(databasesNames)

This will show you only the DATABASES names in cypher-shell:这将只显示 cypher-shell 中的 DATABASES 名称:

  SHOW DATABASES yield name;

Example from python:来自 python 的示例:

test.py测试.py

from neo4j import GraphDatabase

driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password"))

with driver.session() as session:
    result = session.run("SHOW DATABASES yield name;")
    for line in result:
        print(line['name'])

When executed:执行时:

 python test.py 
 neo4j
 system
  

Tested in neo4j 4.2.1 , openjdk 11.0.15 2022-04-19 , python 3.7.0 , Ubuntu 20.04.4在 neo4j 4.2.1、openjdk 11.0.15 2022-04-19、python 3.7.0、Ubuntu 20.04.4 中测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM