views:

167

answers:

1

Hi,

I am very new to sql and more over GQL used in Google app engine. I have managed to push my data to the Google servers but I am not able to figure out a way to query from the entities I've loaded to the datastrore.

Here's the problem, i want to query only the names and numbers of a user's contacts marked by user id apart from other columns like given below,

Name Number       User_id Country    
Rob  0065114146   654351  singapore
Mark 0065132411   654351  singapore
Tina 0065221916   846611  singapore
Nick 0065214126   846611  singapore

This is my Models.py file in which I have my MyContacts Entity definition,

Class MyContacts(db.Model): 
  Name = db.StringProperty(required=True) 
  Number = db.PhoneNumberProperty(required=True) 
  User_id = db.IntegerProperty(required=True) 
  Country = db.StringProperty()

I am taking the user id as input from the UI but I am not able to figure out a way to bring up the data using the query. Please if any one can help me coin the query in GQL it would be a great help for me. I've tried all sorts like SELECT * FROM MyContacts WHERE __key__ = KEY('user_id',654351) but i get nothing.

Please help me out.

+1  A: 

Since User_id is an IntegerProperty, you need to query like this:

db.GqlQuery("SELECT * FROM MyContacts WHERE User_id = :1", some_int)

or equivalently with a query:

MyContacts.all().filter('User_id =', some_int)

Note that if 'User_id' is taken from a Users API User object, you should not be casting it to an int - it's not guaranteed to fit in 64 bits, so you should either be storing it as a string or as a UserProperty.

Nick Johnson
Is there a way to retrieve only the two columns "Name" and "Number" instead of all the columns as I want to show only the name and his number on the UI, so that I can get faster response.
Arun
No - entities are always returned as a whole. You can denormalize into separate tables, but it's really not necessary for a model as small as this one.
Nick Johnson