Rahul Lohra
Rahul Lohra

Reputation: 854

Android sqlite Cursor.getCount() always return 1

I know Cursor.getCount() returns number of columns that's it . I have written a code i can't see where i am making mistake. Please help me I have wasted 4 hours in it .

Cursor res = db.rawQuery("select count(*) from "+TABLE_NAME+" WHERE SENDERS_EMAIL = '"+email+"' AND FRIENDS_STATUS = 1;",null);

    Log.d("FrindsInDb","FriendExists, count="+res.getCount());

Explanation: case 1: if email = [email protected] , then output -> res.getCount() returns 1 (CORRECT) case 2: if email = [email protected] , then ALSO output -> res.getCount() returns 1 (WHY ?)

in my table under SENDERS_EMAIL, only [email protected] exists

Upvotes: 1

Views: 4556

Answers (4)

Xu Wang
Xu Wang

Reputation: 1

cursor.getCount(), return the rows of the result; so, your sql "select count(*) from table" ,which result only one row. you can get count with "select * from table", else you can use "cursor.

Upvotes: 0

Apurva Pagare
Apurva Pagare

Reputation: 26

You are using

Log.d("FrindsInDb","FriendExists, count="+res.getCount());

Here res.getCount() will always return how many rows are there.

Upvotes: 0

Amy
Amy

Reputation: 4032

select count(*) always returning 1 row. Remove count method from your query.

So Select all rows from table then getCount from cursor.

    Cursor res = db.rawQuery("select * from "+TABLE_NAME+" WHERE SENDERS_EMAIL = '"+email+"' AND FRIENDS_STATUS = 1;",null);

    Log.d("FrindsInDb","FriendExists, count="+res.getCount());

Upvotes: 3

Deepak
Deepak

Reputation: 774

res.getCount() will return no. of rows. Since you are asking for count(*) it will always return 1 row, whether data exist or not

proper code will be

Log.d("Result",res.get(0));

here res.get(0) have no. of rows

Upvotes: 2

Related Questions