在python中neo4j.v1 cypher 中Label和Relationship能否传入变量?
发布于 7 年前 作者 james 4496 次浏览 来自 问答

driver = GraphDatabase.driver(“bolt://localhost:7687”, auth=basic_auth(“neo4j”, “neo4j”)) session = driver.session() cypher_staement ="""“ match (a:$label)-[r:$relationship]->(b) where id(a) = $id return b ”"" result = session.run(cypher_staement, label=“Person”, relationship=“KNOW”, id = 123)

以上代码出错,请问各位高手,有没有什么办法实现Label和Relationship支持变量传入?

4 回复

我没用过Python,但那个cypher语句是字符串啊,你怎样拼接$label 和 $relationship 不是很自由吗? 直接拼接 match (a:‘+ labelname +’)-[r:’+relationshipname+’]->(b) 这样也可以啊,我用Nodejs也是这么做的

@pangguoming 谢谢!你的建议我试验了Python中可以实现!已经解决问题,非常感谢! 但这样写可读性不好,如果能够使用neo4j.v1给的传参数方法就更好了。

拼 cypher 字符串 很常见啊,就像拼 SQL一样

python 有没有 string 的format方法? 类似 String.format("match (a:{0})-[r:{1}’]->(b) ",labelname ,relationshipname),这样也可以啊, 其他就看你采用的Python Neo4j包提供啥传参方式没

回到顶部