Gadheyan .t.s
Gadheyan .t.s

Reputation: 407

How to check a record returned in py2neo is null?

I want to check if a node already exist in neo4j. I use the following code:

from py2neo import Graph
from py2neo import *
authenticate("localhost:7474","neo4j", "somepassword")
graph = Graph("http://localhost:7474/db/data/")
graph.cypher.execute("CREATE(alice:Person {name:'Alice' , age:15})->[:Friendof]->(bob:Person {name: 'Bob' , age:14})")
for record in graph.cypher.execute("MATCH(p {title:'The Movie'})RETURN p"):
    if (record[0]==None):
        print "None"
    else:
        print (record[0])

I dont have a movie with title 'The Movie', but i dont get "None" printed. Is there some other way to do this?

Upvotes: 1

Views: 957

Answers (1)

William Lyon
William Lyon

Reputation: 8556

"None" isn't printed because there are no records to iterate through so the print statement isn't executed.

Instead you can check if there are any records returned:

results = graph.cypher.execute("MATCH (p:Movie {title:'The Movie'}) RETURN p")
if results:
    for record in results:
        # do something with each record...
else:
    print "None"

Upvotes: 2

Related Questions