简体   繁体   中英

How to search on SQLITE database android using OR condition?

I am having trouble with searching on sqlite android.

My input types are Name, PhoneNumber,Code.

What I want is whenever the user type any of the following type of input in the search bar it should check whether it is present on any of the given column and return the value.

For example, If the user input the name ALAN then ALAN should return. If the user input the code Jaxxx1 then it should search for the value Jaxxx1 and return its corresponding data. If the user search using phone number then it should return its corresponding data.

What I am getting is nothing. Below is my code somebody please help me.

public List<Contacts> search(String keyword) {
        List<Contacts> contacts = null;
        try {
            SQLiteDatabase sqLiteDatabase = getReadableDatabase();
            Cursor cursor = sqLiteDatabase.rawQuery("select * from " + TABLE_CONTACTS + " where (" + COLUMN_NAME+" or "+COLUMN_CODE +" or "+COLUMN_PNO + ") like ?", new String[]{"%" + keyword + "%"});
            if (cursor.moveToFirst()) {
                contacts = new ArrayList<>();
                do {
                    int id = Integer.parseInt(cursor.getString(0));
                    String cardNo = cursor.getString(1);
                    String name = cursor.getString(2);
                    String jacode = cursor.getString(3);
                    String phno = cursor.getString(4);
                    

                    Contacts contact = new Contacts();
                    contact.setId(id);
                    contact.setCardID(cardNo);
                    contact.setName(name);
                    contact.setJacode(jacode);
                    contact.setPhno(phno);
                    
                    //     Toast.makeText(context,contact.getBalance(),Toast.LENGTH_SHORT).show();


                    contacts.add(new Contacts(id, cardNo, name, jacode, phno, cplan, rent, balance, zone, oldBalance, paidDate, paidAmount, status));
                } while (cursor.moveToNext());
            }
        } catch (Exception e) {
            contacts = null;
        }
        return contacts;
    }

I'm a newbie here, please help me

I think you made a mistake in this line

Cursor cursor = sqLiteDatabase.rawQuery("select * from " + TABLE_CONTACTS + " where (" + COLUMN_NAME+" or "+COLUMN_CODE +" or "+COLUMN_PNO + ") like ?", new String[]{"%" + keyword + "%"});

Your statement should be like this:

"select * from " + TABLE_CONTACTS + " where " + COLUMN_NAME + " like ?" + " or " + COLUMN_CODE + " like ?" + " or " + COLUMN_PNO +" like ?", new String[]{"%" + keyword + "%","%" + keyword + "%","%" + keyword + "%"});

Please check statement (if I forgot double quotation mark). And to get the result you should always pay attention to what user type(upper case or lower case) so you can use some functions already built-in in SQLite.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM