2 回答

TA貢獻1804條經(jīng)驗 獲得超3個贊
雖然在py2neo中沒有直接從文件中讀取的內(nèi)置選項,但有一種機制可以根據(jù)需要獲取參數(shù)序列。因此,剩下的只是使用一個函數(shù)從文件中讀取查詢并使用參數(shù)。這應(yīng)該看起來像這樣:
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
添加回答
舉報