如何在查询功能中使用LIKE子句 [英] How to use LIKE clause in query function

查看:69
本文介绍了如何在查询功能中使用LIKE子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个基于发件人编号在收件箱中搜索邮件的应用程序.这是我的代码:

I am making an application which search messages in the inbox based on sender number. Here is my code:

public void onClick(View v)
{
    Toast.makeText(this, "search", Toast.LENGTH_LONG).show();
    final Uri SMS_INBOX = Uri.parse("content://sms/inbox");

    String[] colList = {"address", "body"};

    Cursor c = getContentResolver().query(SMS_INBOX, colList, null, null,"DATE desc");

    String yz= String.valueOf(c.getCount());
    Toast.makeText(this, yz, Toast.LENGTH_LONG).show();

    if(c.moveToFirst())
    {
        Toast.makeText(this, searchtxt.getText(), Toast.LENGTH_LONG).show();

        for(int i=0;i<c.getCount();i++)
        {
            String number=c.getString(c.getColumnIndexOrThrow("address")).toString();
            boolean match = number.toUpperCase().indexOf(searchtxt.getText().toString()) != -1; 

            if (match)
            {
                Toast.makeText(this, String.valueOf(i), Toast.LENGTH_LONG).show();
                String  body= c.getString(c.getColumnIndexOrThrow("body")).toString();
                tv.setText(tv.getText() + "\n" + body);
            }
            c.moveToNext();
        }
    }
    else
    {
        Toast.makeText(this, "Inside else", Toast.LENGTH_LONG).show();
    }

    c.close();
}

上面的代码可以正常工作,但是它可以检索收件箱中的所有邮件.我希望它只检索与发件人号码匹配的邮件.为此,我尝试使用LIKE子句进行查询,但该查询返回0条记录.使用LIKE子句的正确方法是什么?这是我尝试使用LIKE的代码

The above code works fine but it retrieves all the messages in the inbox. I want that it should retrieve only those messages which match the sender number. For that I tried a query using LIKE clause, but with that it returns 0 records. What is the correct way of using the LIKE clause. Here is the code that I tried using LIKE

String[] colList = {"address", "body"};
String[] argList = {"'%"+searchtxt.getText().toString()+"%'"};
Cursor c = getContentResolver().query(SMS_INBOX, colList, "address LIKE ?", argList,"DATE desc");

String yz= String.valueOf(c.getCount());
Toast.makeText(this, yz, Toast.LENGTH_LONG).show();

请帮帮我....谢谢...

Please help me out....Thank you...

推荐答案

有两个小丑:%"和"_".

There is two joker : "%" and "_".

%"替换任何字符串(多个字符)

"%" is replacing any string (multiple characters),

"_"正在替换一个(并且只有一个)字符.

"_" is replacing one (and only one) character.

例如:

LIKE 'B%' 

让您找到以"B"开头的所有内容.(例如"BATMAN");

let you find everything which starts with "B". (like "BATMAN");

LIKE '%B%'

让您找到包含"B"的所有内容.(如"ABBA");

let you find everthing which contains "B". (like "ABBA");

LIKE'_B%'

让您找到包含一个字符,然后是"B"的所有内容.(例如"ABERCROMBIE").

let you find everything which contains ONE caracter, then a "B". (like "ABERCROMBIE)".

如果要检索包含%"或"_"的字符串,则可以使用转义字符:

if you want to retrieve a string which contains "%" or "_", you may use an escape caracter :

LIKE '%#_B%' ESCAPE #

让您找到任何包含一个"_"后跟一个"B"的字符串.(例如"TEST_BATMAN").

let you find any strings which contains one "_" followed by a "B". (like "TEST_BATMAN").

我希望能有所帮助.

Al_th

这篇关于如何在查询功能中使用LIKE子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆