0

I'm using this code to check if a row exists in my table:

public boolean checkIdiomInFavorite(Idiom idiom){
        String query = "SELECT *" +  " from " + IdiomsOpenHelper.TABLE_FAVORITE +
                " where " + IdiomsOpenHelper.COLUMN_FAV_ID + " = ?;";
        Cursor cursor = database.rawQuery(query,new String[] { String.valueOf(idiom.getId()) });
        if(cursor.moveToNext()){
            return true;
        }
        else{ 
            return false;
        }
    }

But always I get false.
Any idea?

0

3 Answers 3

1

I'd replace this

if(cursor.moveToNext()){
        return true;
    }
    else{ 
        return false;
    }

with

return(cursor.moveToFirst());
Sign up to request clarification or add additional context in comments.

12 Comments

Does the record exist in the specified table, with that condition matched?
Yes, I've tested in database, it returns the record.
What does idiom.getId() return?
So, does this query String query = "SELECT * FROM " + IdiomsOpenHelper.TABLE_FAVORITE + " WHERE " + IdiomsOpenHelper.COLUMN_FAV_ID + " = 5"; work?
Yes it does, I test this query, it returns the record: codeSELECT * FROM Favorite WHERE _id = 5code
|
1

Change this query string...

String query = "SELECT *" +  " from " + IdiomsOpenHelper.TABLE_FAVORITE +
            " where " + IdiomsOpenHelper.COLUMN_FAV_ID + " = ?;";

...to...

String query = "SELECT * from " + IdiomsOpenHelper.TABLE_FAVORITE +
            " where " + IdiomsOpenHelper.COLUMN_FAV_ID = " + idiom.getId();

Then change this line...

Cursor cursor = database.rawQuery(query,new String[] { String.valueOf(idiom.getId()) });

...to...

Cursor cursor = database.rawQuery(query, null);

Comments

0

use the code below:

public boolean checkIdiomInFavorite(Idiom idiom){
        String query = "SELECT *" +  " from " + IdiomsOpenHelper.TABLE_FAVORITE +
                " where " + IdiomsOpenHelper.COLUMN_FAV_ID + " = ?;";
        Cursor cursor = database.rawQuery(query,new String[] { String.valueOf(idiom.getId()) });
        if(cursor.moveToNext()){
            return true;
        }
        else{ 
            return false;
        }
    }

can u able to detect the deference? I am using SELECT *, what means all the columns ... or use any column name you actually have in that table.

You also can count the total rows of the table to check if it returns 0... if returns 0 then no data exists.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.