繁体   English   中英

将Python变量的值添加到Cypher语句中

[英]Add value from Python variable to Cypher statement

我正在尝试什么呢? 我在Python中使用Neo4j。 请看我的代码。 我在python变量'random'中保存第一个cypher语句的结果。 我想在我的第二个cypher语句cypher2中添加这个随机变量的值。 - 我知道cypher2的语法只是添加随机的无意义。 请将其视为表示我的目标的伪代码。 如果有的话,如何将随机值添加到Cypher语句中? 谢谢!

#gives me a random element
cypher1 = "MATCH (n:Event) WITH n, rand() AS r ORDER BY r RETURN n        LIMIT 1"
#I want to add the value of the random element in the cypher     statement
 cypher2 = "MATCH (n:Event)-[:NEXT]->(m:Event) WHERE n = random    RETURN  m.time"
with driver.session() as session:
random = session.run(cypher1)
#i want to mesaure execution time from here without calculating    time for finding random element 
result = session.run(cypher2)
#Execute a given query
def executeCypher(cypher):
    tx = session.begin_transaction()
    result = tx.run(cypher)
    tx.commit()
    return result


#gives me a random element
cypher1 = "MATCH (n:Event) WITH n, rand() AS r ORDER BY r RETURN ID(n) LIMIT 1"
result = executeCypher(cypher1)
id = result.single()[0]
#I want to add the value of the random element in the cypher     statement
cypher2 = """MATCH (n)-[:NEXT]->(m:Event) WHERE ID(n) = %d    RETURN  m.time""" % (id)
executeCypher(cypher2)

此链接可以帮助您: https//neo4j.com/docs/driver-manual/1.7/cypher-values/#driver-result

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM