Home > appengine > appengine – SELECT * From my_model WHERE category LIKE ‘news%’

appengine – SELECT * From my_model WHERE category LIKE ‘news%’

Problem:
I needed to use a LIKE query in appengine.

Solution:
Got some great answers from google search.

Source:
The solution I went with is here.
http://pythoughts.com/app-engine-datastore-workarounds-and-search/

From pythoughts.com

Where in sql you usually would query:

"SELECT * FROM thought WHERE user LIKE gra%"

We now have to use:

"SELECT * FROM thought WHERE user >= 'gra' AND user < :1", u'gra' + u'\ufffd'"

Imagine the table as being sorted alphabetically, user >= ‘gra’ would include any user-names that come after, or equal, ‘G’, then ‘R’, then ‘A’ in the alphabet. The next filter is a less-than inequality, so it’s going to filter out any values that are greater than the specified term, which we set as u’gra’ + u’\ufffd’. The unicode character and ‘\ufffd’ code specify that the result must include the first three character, ‘gra’ (formatted as unicode in order to concatenate with “u’\ufff'”), as well as any other unicode characters following that. This will limit the query enough to return a value such as ‘graeme’, if the search term is ‘grae’, but will not return ‘graham’, or, say, ‘tagrae’.

NB.
1. This works pretty well as the amount of data that I have will be “relatively small”.
2. The data saved in category field is in lowercase, without spaces or punctuation for easier search. Above solution is case sensitive.

Advertisements
Categories: appengine Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: