farah
farah

Reputation: 11

I am using py2neo to connect python to neo4j database

I am using py2neo to connect python to a neo4j database.Then I am trying to execute a query to set up a label name to some nodes, but the label name is a parameter. This is my code

for nodeID in nodesIDs:
    nodes=nodesIDs[nodeID]
    r=graph.cypher.execute("MATCH (d:node00) WHERE d.name in{x} SET d:{ID} RETURN d.name",{"x":nodes,"ID":nodeID})
    print len(r)

but this give me an error said the "Invalid input '(': expected whitespace or a label name"

please advise

Upvotes: 1

Views: 253

Answers (2)

William Lyon
William Lyon

Reputation: 8556

Let's look at the query you are executing:

MATCH (d:node00) 
WHERE d.name IN {x}
SET d:{ID}
RETURN d.name

Where the parameter x is presumably an array of strings, and the parameter ID is a string.

Here are some points to consider:

  1. Node labels cannot be parameterized in a Cypher query. Therefore the clause SET d:{ID} will not work. Instead, use string concatenation within your python script to include the label as part of the string query, not as a parameter: "...SET d:" + str(newLabel) + ...
  2. Make sure the parameter x is actually an array of strings and be sure you have proper spacing in the clause WHERE d.name IN {x} (space between IN and {x})
  3. Finally, you might want to reconsider how you are using labels. It looks like you are treating the label as an ID that identifies an individual node. Labels should be used to identify the type or "class" of Nodes, such as "Person" or "Event", etc.

Upvotes: 0

Reed Jessen
Reed Jessen

Reputation: 141

White space is significant in Cypher query. Make sure that your query actually runs in the Neo4j shell before throwing it into the Python wrapper.

Upvotes: 1

Related Questions