2 回答
TA贡献1804条经验 获得超3个赞
虽然在py2neo中没有直接从文件中读取的内置选项,但有一种机制可以根据需要获取参数序列。因此,剩下的只是使用一个函数从文件中读取查询并使用参数。这应该看起来像这样:
from py2neo import Graph
graph = Graph(password = "*****")
def run_query_from_file(cypher_file_path, parameters=None, **kwparameters):
with open(cypher_file_path, 'r') as cypher_file:
cypher_query = cypher_file.read().strip()
graph.run(cypher_query, parameters)
def test1(dict_of_parameters):
result = run_query_from_file("some_cypher.cypher", dict_of_parameters)
return result
def test2(**kwparameters):
result = run_query_from_file("some_cypher.cypher", **kwparameters)
return result
# Both should work
test1({'username': 'abc', 'password': '123'})
test2('username'='abc', 'password'='123')
其中包含:some_cypher.cypher
MERGE (user:User {username:$username}) with user, user.password as user_password SET user.password = $password RETURN user_password
添加回答
举报