D. Ace February 2016

How to write a query with a AND condition as part of ContentResolver query - Android Content Provider

I have this content resolver query which translates to select * from book where favflag = 1.

But now I want to change it so that it translate to select * from book where favflag = 1 AND bookid = 2;

How do I achieve this?

Here is the content resolver query:

Cursor BookCursor = getActivity().getContentResolver().query(
    BookContract.BookEntry.CONTENT_URI,
    null,
    BookContract.BookEntry.COLUMNFAVFLAG + " = ?",
    new String[]{"1"},
    null);

Answers


CommonsWare February 2016

Since there is no BookContract in the Android SDK that I am aware of, I assume this is your own class. In that case, we do not know the API of that class.

With that in mind, try:

Cursor BookCursor = getActivity().getContentResolver().query(
  BookContract.BookEntry.CONTENT_URI,
  null,
  BookContract.BookEntry.COLUMNFAVFLAG + " = ? AND "+BookContract.BookEntry.COLUMNID + " = ?",
  new String[]{"1", "2"},
  null);

Substitute in the proper value for BookContract.BookEntry.COLUMNID.


Rohit5k2 February 2016

Try this

Cursor BookCursor = getActivity().getContentResolver().query(
    BookContract.BookEntry.CONTENT_URI,
    null,
    BookContract.BookEntry.COLUMNFAVFLAG + " = ? AND "+BookContract.BookEntry.COLUMNBOOKID + " = ?", // Use your variable here in place of COLUMNBOOKID
    new String[]{"1", "2"},
    null);

Post Status

Asked in February 2016
Viewed 3,190 times
Voted 4
Answered 2 times

Search




Leave an answer