3

I have an Email_Message class in my android app that stores email messages. It has an int field called "messageType". messageType == 1 means it's an inbox email, messageType == 2 means it's a sent email.

I just started with Sugar ORM and I want to check in a class how many inbox email do I have stored in the database. More accurately I want to check if I have inbox emails stored there or not, deciding if I need to fetch emails from network or database. But I cant make this if statemant work:

if((int)Email_Message.count(Email_Message.class, "messageType = ?", "1") == 0){} //no emails stored

It says the 3rd argument needs to be a String[] and I dont understand what should I put there then, the only example I could find for usage is this one. (long numberOfAuthors = Author.count(Author.class, "full_name = ?", "Timothy");)

Can someone explain to me how to use the count method correctly?

EDIT: I have to go now, but later I'll check and update the question if String[] test = {"1"}; if((int)Email_Message.count(Email_Message.class, "messageType = ?", test) == 0) does the trick or not.

logi0517
  • 813
  • 1
  • 13
  • 32

2 Answers2

3
if((int)Email_Message.count(Email_Message.class, "message_type = 1", null) == 0)

This worked for me, turns out Sugar ORM also likes to rename the variables like this.

logi0517
  • 813
  • 1
  • 13
  • 32
  • You shouldn't get into the habit of specifying the rvalue for searches in the query text, use the ? operator and an array of values like in @suyang's response for safety. – mikebabcock Nov 17 '17 at 17:20
3

One example to use String[]:

String[] vals = {
    String.valueOf(LocalRecord.ImageState.Raw)
};
long n_count = HourFolder.count(HourFolder.class, "state = ?", vals);
mikebabcock
  • 791
  • 1
  • 7
  • 20