1

I've got the Android Search Dialog working, but the issue I'm having is that I have a 3 column DB table where I'd like all 3 columns to be searched. The below method only performs a search on Column 1 of the table:

//--- GET RECORDS FOR SEARCH
public Cursor searchDB(String query) {
    String[] allColumns = new String[]{ KEY_COLUMN1, KEY_COLUMN2, KEY_COLUMN3 };
    return db.query(true, DB_TABLE, new String[] { KEY_ROWID,
            KEY_COLUMN1, KEY_COLUMN2, KEY_COLUMN3 }, KEY_COLUMN1 + " LIKE" + "'%"   + query + "%'", null, null, null, null, null);
}
//--- END Get Records for Search

So I tried adding a string array to the query like this:

//--- GET RECORDS FOR SEARCH
public Cursor searchDB(String query) {
    String[] allColumns = new String[]{ KEY_COLUMN1, KEY_COLUMN2, KEY_COLUMN3 };
    return db.query(true, DB_TABLE, new String[] { KEY_ROWID,
            KEY_COLUMN1, KEY_COLUMN2, KEY_COLUMN3 }, allColumns + " LIKE" + "'%"        + query + "%'", null, null, null, null, null);
}
//--- END Get Records for Search 

but that crashes the app. So how can the method be modified to search three columns (Column1, Column2, and Column3)?

3 Answers 3

6

You can do that like this (assuming you want all rows that match at least one column):

public Cursor searchDB(String query) { 
    return db.query(true, DB_TABLE, new String[] { KEY_ROWID, KEY_COLUMN1, KEY_COLUMN2,
    KEY_COLUMN3 }, KEY_COLUMN1 + " LIKE" + "'%" + query + "%' OR " + KEY_COLUMN2 + 
    " LIKE" + "'%" + query + "%' OR " + KEY_COLUMN3 + " LIKE" + "'%" + query + "%'", 
    null, null, null, null, null); 
} 
Sign up to request clarification or add additional context in comments.

Comments

0

you need to pass "Null" in place of "allColumns". That will serach all the columns in the row

1 Comment

passing null causes the search to return nothing. But I see where you were going with that....
0
allColumns + " LIKE" + "'%"        + query + "%'"

I think you need to try something like this

column1 + " LIKE '%' AND "+column2        + " LIKE '%' AND " etc.,

Note: There may be syntax errors. It is hand typed.

1 Comment

I'm still trying combos based on your suggestion. The query doesn't like AND though. So I tried KEY_COLUMN1 + KEY_COLUMN2 + KEY_COLUMN3 + " LIKE" + "'%" + query + "%'" and that crashes the app. Still working on it though.....

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.