Writing a query with ORMLite

Majid picture Majid · Aug 10, 2011 · Viewed 26.3k times · Source

How can I write a query with ormlite instead of using .create or any other thing like that? Can you please show me how for this simple example :

SELECT name FROM client

EDIT since I can't answer myself : I guess I had to search a little more , anyway I found how to do it with the QueryBuilder like this :

newDao.query(newDao.queryBuilder().where.eq("name",valueofname)

If someone knows how to write the full query that would be great , otherwise , I'll stick with this solution

Answer

Gray picture Gray · Aug 11, 2011

How can I write a query with ormlite instead of using .create or any other thing like that?

Goodness, there are tons of documentation about how to do this on the ORMLite site. Here's the section on the query builder.

I'm not sure what you mean by "full query" but your example will work with some tweaks:

List<...> results = newDao.queryBuilder().where().eq("name",valueofname).query();

It does not make sense to just return the name since the Dao hierarchy is designed to return the specific Client object. If you just want the name the you can specify the name column only to return:

... clientDao.queryBuilder().selectColumns("name").where()...

That will return a list of Client objects with just the name field (and the id field if it exists) extracted from the database.

If you just want the name strings then you can use the RawResults feature.