views:

76

answers:

2

Hi,

I am using Google App Engine for a project and I need to do some queries on the database. I use the JDOQL to ask the database. In my case I want to obtain the university that contains the substring "array". I think my query has a mistake because it returns the name of universities in the alphabetical order and not the ones containing the substring.

Query query = pm.newQuery("SELECT FROM " + University.class.getName() + " WHERE name.contains("+array+") ORDER BY name RANGE 0, 5");

Could someone tell me what's wrong in my query?

Thank you for your help!

EDIT

I have a list of universities store and I have a suggestbox where we can request a university by his name. And I want to autocomplete the requested name.

A: 

The correct way to do this is like this -

Query query = pm.newQuery(University.class,":p.contains(name)");
query.setOrdering("name asc");
query.setRange(0, 5);
List univs = q.execute(Arrays.asList(array)); 

(note- In this case the :p is an implicit param name you can replace with any name)

Gopi
You can replace it with any random name e.g. ":michael". If there is only one parameter in the query you dont need to declare it and your 'array' will be considered in place of that parameter. see 'implicit parameters' in the link i put in my answer.
Gopi
It doesn't work if array is a substring of a university name. For example if array = 'Harv' it will return nothing but if array is exactly equal to the NAME of a university stored in the DB it works. If array = 'Harvard University' it works...
Michaël
yes. I dont think there is a way in app-engine for searching substring or something like a 'LIKE' in sql.
Gopi
A: 

App engine does not support full-text searches, you should star issue 217. However, A partial workaround is possible. And in your case I think it is a good fit.

First thing, adjust your model such that there is a lower (or upper case) version of the name as well -- I will assume it is called lname. Unless you want your queries to be case-sensitive.

Then you query like this:

Query query = pm.newQuery(University.class);
query.setFilter("lname >= startNameParam");
query.setFilter("lname < stopNameParam");
query.setOrdering("lname asc");
query.declareParameters("String startNameParam");
query.declareParameters("String stopNameParam");
query.setRange(0, 5);
List<University> results = (List<University>) query.execute(search_value, search_value + "z");
Robert Kluin