views:

153

answers:

1

Hello, 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.

+1  A: 

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.

Wooble
That worked! Thank you very much!
Zack Fernandes
No! Don't check for results by calling q.count() - this requires executing the same query twice - once to get the count, and once to get the results.
Nick Johnson