Search code examples
javaandroidsqlsqliteandroid-sqlite

Android sqlite searching two columns


I am getting a bit confused on how to search two separate columns. I am using sqlite.

Cursor cursor = qb.query(db, sqlSelect, "firstName LIKE ? OR lastName LIKE ?" , new String[]{"%" + name + "%", "%" + name + "%"}, null, null, null);

When i search using for say Joe Bob, with that query i can do

'Joe' returns Joe Bob

'Bob' returns Joe Bob

But not 'Joe Bob'

So just want to find a way to search for firstName ' ' lastName but i am having no luck


Solution

  • You need only 1 argument:

    Cursor cursor = qb.query(
        db, 
        sqlSelect, 
        "firstName || ' ' || lastName LIKE ?", 
        new String[]{"%" + name + "%"}, 
        null, null, null
    );