QueryBuilder subquery?

1,079 views
Skip to first unread message

Javi

unread,
Feb 4, 2011, 6:11:15 PM2/4/11
to ORMLite Users
Hi, sorry for my poor english.
I have an issue about QueryBuilder object. I'm using ORMLite for a
Android application.
I want to buid a sql query using QueryBuilder. The query is something
like this:

Select * from Table1
where field1 in (select field1 from Table2 where field2 = 4)

I don't know if I have to use queryForAllRaw or I can build two
queriBuilder objects and pass one of them as parameters like this

dao1.queryBuilder.where(dao2.queryBuilder.prepare());
dao1.queryBuilder.prepare();

Can you help me please?

Thanks in advance

Larry Hamel

unread,
Feb 4, 2011, 6:30:48 PM2/4/11
to ormlit...@googlegroups.com
I think you have to use a direct db call for that. for sqlite, I had to use the IN clause:

"DELETE FROM " + sample.getTableName()
+ " WHERE " + foo
+ " IN ( "
+ " SELECT " + id
+ " FROM " + TABLE
+ " WHERE " + baz + " = " + temp.get_id()
+ " )"

and do a direct sqlite exec call:

public void exec(String sql) {
try {
this.db.execSQL(sql);
} catch (Exception e) {
getLogger().log(Level.SEVERE, "problem exec: ", e);

Gray Watson

unread,
Feb 4, 2011, 7:23:14 PM2/4/11
to ormlit...@googlegroups.com
Ok. I just added this functionality. It will be in 4.11. Should have done it earlier. It was pretty easy to fit in. Sample code from the tests is at the bottom.

Comments?
gray

--------------------

QueryBuilder<Basic, String> bqb = basicDao.queryBuilder();
// must have only 1 field
bqb.selectColumns(Basic.ID_FIELD);

bqb.where().eq(Basic.ID_FIELD, string1);
// the field above must match the Foo field name but there is no internal checking
List<Foo> results = fooDao.query(fooDao.queryBuilder().where().in(Foo.STUFF_FIELD_NAME, bqb).prepare());
...

Larry Hamel

unread,
Feb 4, 2011, 9:14:02 PM2/4/11
to ormlit...@googlegroups.com
value++

Gray

unread,
Feb 15, 2011, 1:02:08 PM2/15/11
to ORMLite Users
Just to close this question, sub queries (IN and EXISTS) were released
in version 4.11. Here are the docs:

http://ormlite.com/docs/inner-queries

gray

Javier Hinmel

unread,
Feb 16, 2011, 8:42:10 AM2/16/11
to ormlit...@googlegroups.com
Ok, thank you
Reply all
Reply to author
Forward
0 new messages