亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Cypher 文件作為 py2neo python 中的參數

Cypher 文件作為 py2neo python 中的參數

慕勒3428872 2022-08-16 10:11:15
我正在嘗試在py2neo中將文件作為參數傳遞,以使查詢中的變量作為其他參數。而不是:cypherfrom py2neo import Graphgraph = Graph(password = "*****")def test(some_things):    result = graph.run(                "MATCH (movie:movies)"                "where movie.name =~ $name "                "RETURN movie",{"name":"(?i).*" + some_things+ ".*"})    return result我想知道是否有這樣的東西:from py2neo import Graphgraph = Graph(password = "*****")def test(some_things):    result = graph.run("some_cypher.cypher", some_things)    return result其中可能是:some_cypher.cypherMATCH (movie:movies) where movie.name =~ $name RETURN movie, ,{"name":"(?i).*" + ?+ ".*"}在 python 文件中是要替換為 的參數。?some_things
查看完整描述

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


查看完整回答
反對 回復 2022-08-16
?
喵喔喔

TA貢獻1735條經驗 獲得超5個贊

py2neo中沒有內置這樣的功能。如果你想這樣做,你必須滾動自己的函數。


查看完整回答
反對 回復 2022-08-16
  • 2 回答
  • 0 關注
  • 98 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號