[英]Add value from Python variable to Cypher statement
Is what I am trying at all possible? 我正在尝试什么呢? I am working with Neo4j within Python.
我在Python中使用Neo4j。 Please see my code.
请看我的代码。 I am saving the result of the first cypher statement in the python variable 'random'.
我在python变量'random'中保存第一个cypher语句的结果。 I want to add the value of this random variable inside my second cypher statement, cypher2.
我想在我的第二个cypher语句cypher2中添加这个随机变量的值。 - I know the syntax of cypher2 is nonsense just adding random.
- 我知道cypher2的语法只是添加随机的无意义。 Please see it as pseudo code expressing my goal.
请将其视为表示我的目标的伪代码。 How can I add the random value to the Cypher statement, if at all?
如果有的话,如何将随机值添加到Cypher语句中? Thanks!
谢谢!
#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)
This link can help you: https://neo4j.com/docs/driver-manual/1.7/cypher-values/#driver-result 此链接可以帮助您: https : //neo4j.com/docs/driver-manual/1.7/cypher-values/#driver-result
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.