0
votes

got another problem now. I'd like to create a working HTML form where user can enter name of the node, and through radioboxes to choose the label - and after submit it is created in neo4j.

Following works:

result = graph.cypher.execute("CREATE (n:Test { name : '%s' })" % cypher_escape(node))

So now I am able to create a node with whatever name I enter, but I can't get the label to work. From the documentation I know that cypher_escape can be used only with 1 argument and that labels cannot be parametrised.

Is there some workaround / other way to do it so I can put variable as a label to the cypher query ?

Thanks in advance ! :)

1

1 Answers

3
votes

Labels cannot be parameterized in Cypher so you must use string concatenation or string formatting in client code to build the Cypher query. The rationale for this is that the query plan could be different depending on the label and therefore the execution plan cannot be cached.

You should however use a query parameter for property values. This allows for query plan caching and better performance when executing the query again:

graph.cypher.execute("CREATE (n:Test {name: {name})", parameters={"name": "Bob"})

So then to use string formatting for the label:

label_string = "Test"
query = "CREATE (n:%s {name: {name})" % label_string
graph.cypher.execute(query, parameters={"name": "Bob"})