如何在安卓中使用OR条件搜索SQLITE数据库?

huangapple go评论75阅读模式
英文:

How to search on SQLITE database android using OR condition?

问题

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);

                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;
}

Note: I've provided the translated code you requested. If you have any further questions or need assistance with this code, please feel free to ask.

英文:

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&lt;Contacts&gt; search(String keyword) {
        List&lt;Contacts&gt; contacts = null;
        try {
            SQLiteDatabase sqLiteDatabase = getReadableDatabase();
            Cursor cursor = sqLiteDatabase.rawQuery(&quot;select * from &quot; + TABLE_CONTACTS + &quot; where (&quot; + COLUMN_NAME+&quot; or &quot;+COLUMN_CODE +&quot; or &quot;+COLUMN_PNO + &quot;) like ?&quot;, new String[]{&quot;%&quot; + keyword + &quot;%&quot;});
            if (cursor.moveToFirst()) {
                contacts = new ArrayList&lt;&gt;();
                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

答案1

得分: 2

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.

英文:

I think you made a mistake in this line

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

Your statement should be like this:

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

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.

huangapple
  • 本文由 发表于 2020年9月5日 02:50:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63746615.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定