sachin
sachin

Reputation: 1

Android Where Clause

I'm trying to write email search application in android , I want to search like

select * from message where subject like '%email%' or fromlist like'%email%'
or displayname like '%email%' or tolist like '%email%' or cclist like '%email%'
or bcclist like '%email%' or replytolist like '%email%';

How to write a where clause in query method in android???

Upvotes: 0

Views: 2551

Answers (2)

Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28541

Use SQLiteDatabase.query like this:

final String emailArg = "%" + email + "%";
final String where = "subject like ? OR fromlist like ?";
final String[] arguments = new String[] {
  emailArg,
  emailArg 
};

Cursor result = db.query("message", null, where, arguments, null, null, null, null);

You can then use the result as you want.

Upvotes: 0

yeradis
yeradis

Reputation: 5347

You can use a Raw Query on SQLite

This is what i do:

public Cursor getFriendsCursorByNickName(String nickname) {
    String[] args = new String[1];
    args[0] = "%"+nickname+"%";
    Cursor friendLike = db.rawQuery("SELECT * FROM friends WHERE nickname like ?", args);
    friendLike.moveToFirst();
    return friendLike;
}

The stuff here is "?" inside the query so you can prepare conditions outside as "args" do I hope this guide you ;)

Upvotes: 1

Related Questions