0

I have some code like this:

    public void gameOverCheck() {
    SQLiteDatabase database = this.getWritableDatabase();
    database.execSQL("SELECT " + COL_ROLE + " COUNT");
    database.close();
}

in android. And would like to count the values of COL_ROLE (which has one of two possible values (mafia, civilian)). So if the number of Mafia >= Civilian game = mafias win. If Mafias = 0, game = civilian won. I am struggling with the SQL command to do the first part, am I supposed to group the values?

1 Answer 1

1

Try this:

Cursor mCount= db.rawQuery("select count(*) from your_table where " + COL_ROLE + " = 'mafia'", null);
mCount.moveToFirst();
int count = mCount.getInt(0);
mCount.close();`
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you :), what do you mean by it will be stored in the Count variable? How would I retrieve this variable and say, store it into a String or an Int?
Sorry, I forgot that the execSql method doesn't return any data - I've updated my answer :-)
Thank you :) So I now have Cursors which count both the Mafias and Civilians using your code, stored in countM and countC respectively. I need an if else statement now to compare the two ints and I should be set! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.