Search code examples
pythonneo4jcypherpy2neo

How to introduce parameters in a cypher query?


I have this cypher query in a Python program:

prevNode = graph_db.data("OPTIONAL MATCH (n:Node) WHERE n.property = {param} RETURN n")

The line of code above gives this error:

'py2neo.database.status.ClientError: Expected a parameter named param'

param is well defined at this point of the program because I print it a line before executing the query.

I have tried to put the value in the query instead of the parameter, like this:

prevNode = graph_db.data("OPTIONAL MATCH (n:Node) WHERE n.property = '1234' RETURN n")

And worked well.

Does anyone knows where is the mistake?

Thanks in advance.


Solution

  • You didn't pass any substitution variables into the function.

    graph_db.data("OPTIONAL MATCH (n:Node) WHERE n.property = {param} RETURN n", param='1234')
    

    You can also explicitly pass in the locals() dictionary; this will use any local variables you have defined of the appropriate names:

    graph_db.data("OPTIONAL MATCH (n:Node) WHERE n.property = {param} RETURN n", **locals())