如何使用官方 Neo4j Python Drive 将字符串变量添加到 SET 请求中?

问题描述 投票:0回答:0

由于py2neo与最新的Neo4j不太兼容,所以我选择使用官方的Neo4j Python驱动程序。

我的目的

我一直在尝试将字符串变量添加到

Driver.execute_query()
方法的 cypher
SET
请求中。

我的目的是使用 Neo4j-python-drive 为某些特定节点提供另一个关键“内容”,并将其值设置为等于输入字符串变量(

node. Content = input_string_variable
)。

即将

(n:Log {id: 'log_001-02'})
变成
(n:Log {id: 'log_001-02', content: my_input_log_string})

现在我的数据库中的每个节点都给出了键“id”的唯一值,因此我通过

(node)
搜索任何
node.id

资讯

python 和 Neo4j 版本

> python -V 
Python 3.11.3
> pip show neo4j
Name: neo4j
Version: 5.10.0
Summary: Neo4j Bolt driver for Python
Home-page:
Author:
Author-email: "Neo4j, Inc." <[email protected]>
License: Apache License, Version 2.0
Location: D:\miniconda3\envs\coderunnew\Lib\site-packages
Requires: pytz
Required-by:

我的代码

import neo4j
from neo4j import GraphDatabase, RoutingControl, Driver
# from neo4j.exceptions import DriverError, Neo4jError
import logging
import sys
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
logging.getLogger("neo4j").addHandler(handler)
logging.getLogger("neo4j").setLevel(logging.DEBUG)
URI = "neo4j://127.0.0.1:7687"
AUTH = ("neo4j", "!neo4j")
database_name = "neo4j"

def add_node(driver: Driver, target_node_id: str):
    driver.execute_query(
        "CREATE (n {id: $target_node_id})",
        target_node_id = target_node_id, 
        database_ = 'neo4j',
    )

def add_node_content(driver: Driver, target_node_isd: str, content_to_be_added: str):
    driver.execute_query(
        "MATCH (n {id: $target_node_id})"
        "SET n.content= $content_to_be_added",
        target_node_id = target_node_id, content_to_be_added = content_to_be_added,
        database_ = 'neo4j',
    )

with GraphDatabase.driver(URI, auth=AUTH, trusted_certificates = neo4j.TrustAll()) as driver:
    driver.verify_connectivity()
    add_node(driver, 'test_id')
    add_node_content(driver, 'test_id', 'content_text')

错误按摩

  File "D:\test_neo4j.py", line 22, in add_node_content
    driver.execute_query(
  File "D:\test_neo4j.py", line 32, in <module>
    add_node_content(driver, 'test_id', 'content_text')
neo4j.exceptions.CypherSyntaxError: {code: Neo.ClientError.Statement.SyntaxError} {message: Invalid input '{': expected "+" or "-" (line 1, column 47 (offset: 46))
"MATCH (n {id: $target_node_id})SET n.content= {content_to_be_added}"
                                               ^}

日志

我选择了一些关于以下错误的日志。

执行第二个查询:

>> [#DAC1]  _: <CONNECTION> state: READY > TX_READY_OR_TX_STREAMING
>> [#DAC1]  C: RUN 'MATCH (n {id: $target_node_id})SET n.content= {content_to_be_added}' {'target_node_id': 'test_id', 'content_to_be_added': 'content_text'} {}
>> [#DAC1]  C: PULL {'n': 1000}
>> [#DAC1]  S: FAILURE {'code': 'Neo.ClientError.Statement.SyntaxError', 'message': 'Invalid input \'{\': expected "+" or "-" (line 1, column 47 (offset: 46))\r\n"MATCH (n {id: $target_node_id})SET n.content= {content_to_be_added}"\r\n                                               ^'}
>> [#DAC1]  C: RESET
>> [#DAC1]  S: IGNORED
>> [#DAC1]  S: SUCCESS {}
>> [#DAC1]  _: <CONNECTION> state: FAILED > READY
python python-3.x neo4j driver neo4j-apoc
© www.soinside.com 2019 - 2024. All rights reserved.