Zubair Rasheed
Zubair Rasheed

Reputation: 49

How to search for string match in specific row of table in database in android

I am building an App and dealing with databases for the first time I am trying to get the data TEXT as input and search its match in the specific row. I have failed to do that.

I have got the specific row selected. I have six columns and I have got the specific row depending upon the id but comparing the Input Text in the other five columns is giving problematic output. With my approach given in the code below it is giving results after comparing it with the first or last column entry but not all.

    public Cursor finddata(String name,int ida, int guess){
        String Query = "SELECT  NAME1 FROM Capital_Names where ID = '" + ida + "' and NAME1='" +name+ "'";
                    cursor=db.rawQuery(Query,null);
                    check=1;
                    if(cursor==null){
                        String Query2 = "SELECT  NAME1 FROM Capital_Names where ID = '" + ida + "' and NAME2='" +name+ "'";
                        cursor=db.rawQuery(Query2,null);
                        check=2;
                    }
                    if(cursor==null){
                        String Query3 = "SELECT  NAME1 FROM Capital_Names where ID = '" + ida + "' and NAME3='" +name+ "'";
                        cursor=db.rawQuery(Query3,null);
                        check=3;
                    }
                    if(cursor==null){
                        String Query4 = "SELECT  NAME1 FROM Capital_Names where ID = '" + ida + "' and NAME4='" +name+ "'";
                        cursor=db.rawQuery(Query4,null);
                        check=4;
                    }
                    if(cursor==null){
                        String Query5 = "SELECT  NAME1 FROM Capital_Names where ID = '" + ida + "' and NAME5='" +name+ "'";
                        cursor=db.rawQuery(Query5,null);
                        check=5;
                    }
                    if(cursor==null){
                        String Query6 = "SELECT  NAME1 FROM Capital_Names where ID = '" + ida + "' and NAME6='" +name+ "'";
                        cursor=db.rawQuery(Query6,null);
                    }
                    return cursor;
                    }

I think there is some error in the if condition that I am applying on the cursor as "if(cursor==null)" because it is only giving the answer by comparing the first statement without if even if it is matching in the first statement or not.

If there is some direct query statement to do that ?????

I have searched a lot on the internet but could not find a solution. And please do not direct me to already answered questions and answer this question if you could. Regards

Upvotes: 1

Views: 950

Answers (2)

forpas
forpas

Reputation: 164089

What you are doing wrong is that you check if the Cursor object returned by rawQuery() is null.
rawQuery() returns a Cursor which may be empty (without any rows) but it can never be null.
You can check if there any rows in the Cursor by checking its moveToFirst() method.
Also never use concatenation of the parameters of the sql statement. It is not safe.
Use placeholders ? and the 2nd argument of rawQuery() to pass the parameters.
So change to this:

public Cursor finddata(String name, int ida, int guess){
    String sql = "SELECT NAME1 FROM Capital_Names " + 
        "WHERE ID = ? AND ? IN (NAME1, NAME2, NAME3, NAME4, NAME5)";
    return db.rawQuery(sql, new String[] {ida, name});
} 

It seems in your code that you don't use the argument guess of finddata(), so you can remove it.
Also are you sure that you want to return only the column NAME1? You can replace it with "SELECT * FROM..." to return all the columns.

Upvotes: 1

Divyesh patel
Divyesh patel

Reputation: 987

public Cursor finddata(String name,int ida, int guess){
    String Query = "SELECT  NAME1 FROM Capital_Names 
                    WHERE ID = '" + ida + "'
                    AND
                   ( 
                   NAME1='" +name+ "' OR NAME2='" +name+ "' OR NAME3='" +name+ "' OR 
                   NAME4='" +name+ "' OR NAME5='" +name+ "')"
                   ;
return db.rawQuery(Query,null);
}

Try this on...

Upvotes: 1

Related Questions