I want to retrieve data from db android but Cursor always retuns null

My issue is that when i try to retrieve data from "select * from TABLE_SEARCH_DETAILS WHERE KEY_SEARCH_PAIR + " 'searchPair' method i am getting the Cursor null.The table has all the values needed. I have checked that using db browser. Code is given below.

 String getCollectedDataQuery = "select * from " + TABLE_SEARCH_DETAILS + " WHERE " + KEY_SEARCH_PAIR + " = '" + searchPair+" '";

        Cursor getCollectedCursor = groceryAppDatabase.rawQuery(getCollectedDataQuery, null);
    if (getCollectedCursor==null){
        Log.v("NULL","=="+getCollectedCursor);
    }

but when i remove where part from Query i gets data.. Anyone knows what is the issue in my "where" part on my query.

2 answers

  • answered 2018-01-11 20:54 Hamze rasaee

    Add ' at the end of line

    String getCollectedDataQuery = "select * from " + TABLE_SEARCH_DETAILS + " WHERE " + KEY_SEARCH_PAIR + " = '" + searchPair + "'";
    
        Cursor getCollectedCursor = groceryAppDatabase.rawQuery(getCollectedDataQuery, null);
    if (getCollectedCursor==null){
        Log.v("NULL","=="+getCollectedCursor);
    }
    

  • answered 2018-01-11 20:54 Robert Nekic

    The second value on rawQuery should be the selectionArgs. Try this:

    String getCollectedDataQuery = "select * from " + TABLE_SEARCH_DETAILS + " WHERE " + KEY_SEARCH_PAIR + " = ?";
    String[] queryArgs = new String[]{searchPair};
    
    
    Cursor getCollectedCursor = groceryAppDatabase.rawQuery(getCollectedDataQuery, queryArgs);