1

I have a database, with names and true or false values. I have for example 10 rows and four of them have got the value "true".

name | value  
-------------  
1    | false  
2    | true  
3    | false  
4    | true  
5    | false  
6    | false  
7    | true  
8    | false  
9    | false  
10   | true

my try is:

val c = db!!.rawQuery("SELECT COUNT(*) FROM table GROUP BY value HAVING value = \"true\"", arrayOf())  
c.moveToNext()  
Log.e("OUTPUT", c.toString())

but the log I will get is:
E/OUTPUT: android.database.sqlite.SQLiteCursor@b0b20ea

So my question is, how to get the countnumber as a usable Integer value?

1
  • Your query seems wrong. If you want to count the number of true lines you should simply use: SELECT COUNT(*) FROM table WHERE value = "true";. If it doesn't work, try value IS true. Commented Aug 5, 2020 at 13:57

1 Answer 1

1

First correct your query like this:

val c = db!!.rawQuery("SELECT COUNT(*) FROM table WHERE value = 'true'"

because you don't want to group by value but count the rows of the tables that contain the value 'true'.
I assume that the column value contains strings.
If it contains booleans then the query should be:

val c = db!!.rawQuery("SELECT COUNT(*) FROM table WHERE value"

Then you can extract the value of the 1st and only column of the cursor by c.getInt(0):

Log.e("OUTPUT", c.getInt(0).toString())
Sign up to request clarification or add additional context in comments.

1 Comment

Jeah now it is working. The query works with \" as well with ' this does not make a different, but i changed it anyway and delete the GROUP BY. So thank you. But the second one works very good. Thank you.

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.