Search code examples
javaandroidselectormlite

Can't compile ORMLite statement


I'm trying to compile ORMLite query statement but I'm getting the following SQLException:

Could not compile this SELECT_LONG statement since the caller is expecting a SELECT statement.  Check your QueryBuilder methods.

I've tried all the possible ways to do so but I'm always getting it, my primary try was:

QueryBuilder<Message, Integer> messagesQueryBuilder = dbManager.getMessagesDao().queryBuilder();   
Where<Message, Integer> resultedQuery = messagesQueryBuilder.where().in(Message.SCHOOL_COLUMN_NAME, schoolIntegerQueryBuilder);
Long count = messagesQueryBuilder.countOf();
messagesQueryBuilder.orderBy(Message.MESSAGE_TIMESTAMP, false);
messagesQueryBuilder.queryForFirst(); // getting it here

And then I tried to prepare the query first:

messagesQueryBuilder.orderBy(Message.MESSAGE_TIMESTAMP, false);
Message message = dbManager.getMessagesDao().query(messagesQueryBuilder.prepare()).get(0); // getting here also 

And finally tried to prepare it separately in a new object then using that object:

PreparedQuery<Message> queryForMessage = messagesQueryBuilder.orderBy(Message.MESSAGE_TIMESTAMP, false).prepare();
Message message = dbManager.getMessagesDao().query(queryForMessage).get(0); // also failing here with the same Exception

I don't know what I'm exactly doing wrong but even though I tried all the possible ways that normally working with me.


Solution

  • Finally found where is the problem, in ORMLite when executing a method you are like executing a statements not only getting values to store it somewhere else, so when executing this:

    Long count = messagesQueryBuilder.countOf();
    

    now the messagesQueryBuilder contains only the retrieved count not the whole query result that was stored in it before.

    Hence, to solve my problem, I reordered the code execution lines to be as the following:

    QueryBuilder<Message, Integer> messagesQueryBuilder = dbManager.getMessagesDao().queryBuilder();   
    Where<Message, Integer> resultedQuery = messagesQueryBuilder.where().in(Message.SCHOOL_COLUMN_NAME, schoolIntegerQueryBuilder);
    
    messagesQueryBuilder.orderBy(Message.MESSAGE_TIMESTAMP, false);
    Message message = messagesQueryBuilder.queryForFirst();
    
    Long count = messagesQueryBuilder.countOf();