Your first query

How do you execute a Cypher statement and access the results?

Execute the Cypher statement

Select the correct method to execute the Cypher statement.

python
driver = GraphDatabase.driver(NEO4J_URI,
    auth=(NEO4J_USER, NEO4J_PASSWORD)
)

cypher = """
MATCH (m:Movie {title: $title})<-[:ACTED_IN]-(p)
RETURN p.name AS actor
"""
params = {"title": "Toy Story"}

res, summary, keys = driver.#select:execute_query(
    query,
    **params
)
  • ❏ cypher(

  • ✓ execute_query(

  • ❏ execute(

  • ❏ verify_connectivity(

Hint

The method to execute a Cypher query with the driver is the same one you used in the previous lesson - it starts with execute_.

Solution

The correct answer is execute_query(). This method executes a Cypher query and returns the results.

python
driver.execute_query(query, **params)

Print the actor names

Update the code to print the name of each actor.

python
for record in res.records:
    print(
        #input:record["actor"]
    )
  • ✓ record["actor"]

Hint

Review the RETURN clause in the Cypher statement.

python
cypher = """
MATCH (m:Movie {title: $title})<-[:ACTED_IN]-(p)
RETURN p.name AS actor
"""

Solution

The name property of the p node is aliased as actor in the RETURN clause.

python
for record in res.records:
    print(record["actor"])

Lesson summary

Nice work! You learned how to execute a Cypher statement and access the results.

In the next lesson, you will review how to transform the results.