3

I am using ActiveAndroid as ORM system in my project and i use this line of code for Query over database

 List<Chats> s = new Select()
                .from(Chats.class)
                .where(col_conversation + " = ?  and " + col_sender + " = ? and " + col_body + " = ?",conversation.getId(),sender.getId(),body)    .execute();

but it fetch 0 row as result. i am sure that i have such row in database.

Navid_pdp11
  • 3,487
  • 3
  • 37
  • 65
  • Post your col_conversation,col_sender,col_body values and your database model class. – romtsn Jan 27 '15 at 11:16
  • thank you... but i checked them and they are correct. when i make my where statement without parameter it work fine but when i send multi parameter to it result is empty – Navid_pdp11 Jan 27 '15 at 11:20
  • What about single parameter? It still working with it? I have a point that maybe you should try `AND` operand in uppercase. – romtsn Jan 27 '15 at 11:24
  • i test with single parameter query and it works fine. but i use **and** instate **AND** in parameter less query but it work fine??? – Navid_pdp11 Jan 27 '15 at 11:35

2 Answers2

6

Another way is to use several where clause to execute multiparameter query:

List<Chats> s = new Select()
            .from(Chats.class)
            .where(col_conversation + " = ?",conversation.getId())
            .where(col_sender + " = ?", sender.getId())
            .where(body + " = ?", body)
            .execute();
romtsn
  • 11,704
  • 2
  • 31
  • 49
  • thank you it was useful but if i need to create _and_ / _or_ statement what should i do ??? – Navid_pdp11 Jan 27 '15 at 11:42
  • This doesn't works man. 2nd where clause returns all records in that particular column. – Noman Rafique Jun 15 '15 at 08:58
  • I need to check my exact sql query (with ? resolved into their respective values), how can I do that? `Select select = new Select();` and then `select.toSql()` shows the query with ? in it.. – Bugs Happen Feb 16 '16 at 06:22
2

you most pass no parameters and change your select query to below :

List<Chats> s = new Select()
                .from(Chats.class)
                .where(col_conversation + " = " + conversation.getId() + " and " + col_sender + " = " + sender.getId() + " and " + col_body + " = '" + body + "'")

                .execute();