简体   繁体   中英

Add value from Python variable to Cypher statement

Is what I am trying at all possible? I am working with Neo4j within Python. Please see my code. I am saving the result of the first cypher statement in the python variable 'random'. I want to add the value of this random variable inside my second cypher statement, cypher2. - I know the syntax of cypher2 is nonsense just adding random. Please see it as pseudo code expressing my goal. How can I add the random value to the Cypher statement, if at all? 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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