【问题标题】:Connecting to Memgraph graph database from python从 python 连接到 Memgraph 图数据库
【发布时间】:2022-11-11 02:31:56
【问题描述】:

从 Python 连接到正在运行的 Memgraph 实例需要什么?

【问题讨论】:

    标签: memgraphdb


    【解决方案1】:

    要使用 Python 连接到 Memgraph,您需要:

    • 一个运行 Memgraph 实例.如果您需要设置 Memgraph,请查看 Installation guide
    • GQLAlchemy client。 Python 编程语言的 Memgraph OGM(对象图映射器)。

    创建一个新的 Python 脚本并向其中添加以下代码: 从 gqlalchemy 导入 Memgraph

    # Make a connection to the database
    memgraph = Memgraph(host='127.0.0.1', port=7687)
    
    # Delete all nodes and relationships
    query = "MATCH (n) DETACH DELETE n"
    
    # Execute the query
    memgraph.execute(query)
    
    # Create a node with the label FirstNode and message property with the value "Hello, World!"
    query = """CREATE (n:FirstNode)
               SET n.message = '{message}'
               RETURN 'Node '  + id(n) + ': ' + n.message AS result""".format(message="Hello, World!")
    
    # Execute the query
    results = memgraph.execute_and_fetch(query)
    
    # Print the first member
    print(list(results)[0]['result'])
    

    您可以在Memgraph documentation 中找到详细说明。

    【讨论】:

      猜你喜欢
      • 2022-11-12
      • 1970-01-01
      • 2022-11-29
      • 2022-11-14
      • 1970-01-01
      • 2015-12-26
      • 1970-01-01
      • 2019-09-22
      相关资源
      最近更新 更多