ORMLite groupByRaw and groupBy problem on android SQLite db

I have the content of a SQLite table with the following columns:

-----------------------------------------------
|id|book_name|chapter_nr|verse_nr|word_nr|word|
-----------------------------------------------

      

sql query

select count(*) from content where book_name = 'John'
group by book_name, chapter_nr

      

in DB Browser returns 21 lines (this is the number of chapters)

equivalent with ORMLite android:

long count = getHelper().getWordDao().queryBuilder()
                        .groupByRaw("book_name, chapter_nr")
                        .where()
                        .eq("book_name", book_name)
                        .countOf(); 

      

returns 828 lines (this is the number of verse numbers)

As far as I know, the above code translates to:

select count(*) from content
where book_name = 'John'
group by book_name, chapter_nr

      

the result of this in the DB browser:

   | count(*)
------------
 1 | 828
 2 | 430
 3 | 653
...
 21| 542
---------
21 Rows returned from: select count(*)...

      

it seems to me that ORMLite is returning the first row of the query in the result countOf()

.

I've searched stackoverflow and google many times. I found this question (and more interestingly the answer)

You can also count the number of rows in a custom query by calling the> countOf () method on a Where or QueryBuilder object.

// count the number of rows in this custom query int numRows = dao.queryBuilder (). where (). eq ("name", "Joe Smith"). countOf ();

this (correct me if i'm wrong) is exactly what i'm doing, but somehow i'm just getting the wrong number of lines.

So ... either I'm doing something wrong or it countOf()

doesn't work as expected.

Note. Same with groupBy

instead groupByRaw

(according to ORMLite documentation joining groupBy should work)

...
.groupBy("book_name")
.groupBy("chapter_nr")
.where(...)
.countOf()

      

EDIT : getWordDao

Returns from class Word

:

@DatabaseTable(tableName = "content")
public class Word { ... }

      

+3


source to share


1 answer


returns 828 lines (this is the number of verse numbers)

This appears to be a limitation of the mechanism QueryBuilder.countOf()

. It expects a single value and doesn't understand adding GROUP BY

count to the query. You can tell that it is not because this method returns one long

.



If you want to extract the counts for each of the groups, it looks like you need to run a raw query to check the docs .

+1


source







All Articles