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__开发者_C百科 = KEY('user_id',654351)
but i get nothing.
Please help me out.
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.
精彩评论