neo4j 如何在py2neo中传递参数给密码查询

e0bqpujr  于 2022-11-05  发布在  其他
关注(0)|答案(4)|浏览(167)

我有一个变量name=“Rahul”,我想以下面的方式将此变量传递给Py 2neo中的cypher query:

line=session.execute("MATCH (person)WHERE person.name=name RETURN person")

但我收到一个错误-

"py2neo.cypher.InvalidSyntax: name not defined (line 1, column 33)"

如何在py 2neo中传递变量

ni65a41a

ni65a41a1#

如果name是一个参数,则需要将其括在大括号中。

MATCH (person) WHERE person.name = {name} RETURN person

您的Python代码可能沿着以下代码行

graph_db = neo4j.GraphDatabaseService()
qs = 'MATCH (person) WHERE person.name = {name} RETURN person'
query = neo4j.CypherQuery(graph_db, qs)
results = query.execute(name='Rahul')
print results
kmpatx3s

kmpatx3s2#

如果你想把你的查询封装在一个事务中,你可以使用cypher模块来创建一个会话,然后创建一个事务对象。实现这一点的语法与neo4j.CypherQuery中的语法略有不同,在stephenmuss的回答中提到过。

from py2neo import neo4j, cypher
graph_db = neo4j.GraphDatabaseService('http://localhost:7474/db/data/')

# Create a Session

session = cypher.Session('http://localhost:7474')

# Create a transaction

tx = session.create_transaction()

# Write your query, and then include it in the transaction with a dictionary

# of parameters.

qs = 'MATCH (person) WHERE person.name = {name} RETURN person'
tx.append(qs, parameters={'name': 'Rahul'})
results = tx.commit()
pgpifvop

pgpifvop3#

获取节点的另一种方法是:

from py2neo import Graph, authenticate

server = "localhost:7474"

# set up authentication parameters

authenticate(server, <user>, <password>)

graph = Graph("{0}/db/data".format(server))
results = graph.find("person", "name", "Rahul")
lc8prwob

lc8prwob4#

为了清楚起见,将参数传递给py 2neo查询的一般答案是:

from py2neo import Graph

graph = Graph("URI", auth=("USER", "PASSWORD"))
graph.run("MATCH (p:Person) WHERE p.name=$name RETURN p", parameters={'name': 'Rahul'})

(Note$符号)

相关问题