Question

I am working on a Google App Engine application, and have been facing some issues with a GQL query and an if statement. This is the code:

q = Song.gql("WHERE title = :1", self.request.get('song_title'))
q.get()
  if q:
    r = "Excisting Results Found: <br />"
    print q
    for song in q:
      r += song.title+" by "+song.artist+"<br />"
    self.response.out.write(r)
  else: 
    ...

When this is run, the page returns "Excisting Results Found:" however I know that no results were in fact found. Is there a way to check if the results returned by the query are empty? What would a blank result returned from GqlQuery look like?

Any help would be greatly appreciated,
Thanks.

Was it helpful?

Solution

In this example, q is a GQL query object. Although you can treat it as an iterable, calling get() on it will return a single result, and you'd need to assign this result to a variable. You can also check if there are results by checking if q.count(1) is greater than 0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top