choco
choco

Reputation: 255

SQLite query with two where

I am trying to get data from my sqlite database, according to two arguments , type and name. I tried a lot of solutions but none of them worked. that is my query

   ...
   mCursor = db.query(false,TABLE_SERVICE, new String[] {KEY_ID,KEY_NAME,
               KEY_ADRESSE, KEY_EMAIL, KEY_NUMTEL, KEY_WEBSITE,KEY_IMAGE},
               KEY_NAME
                + " like ? and " + KEY_TYPE+ " like ?",
                new String[] {inputText,c}, null, null, null,null);
      }
      if (mCursor != null) {
       mCursor.moveToFirst();
      }
      return mCursor;
     ....

i've also tried :

KET_NAME + "=" + inputText+ " AND " + KEY_TYPE + "=" + c

and this

KEY_NAME + " = '" + inputText+ "' AND " + KEY_TYPE + " = '" + c+ "'"

and nothing worked ! i'm getting an empty cursor.

Upvotes: 0

Views: 53

Answers (1)

BladeCoder
BladeCoder

Reputation: 12949

Please note that LIKE doesn't work the same way as =. LIKE takes a matching pattern as parameter, such as "%query%" (meaning anything containing the word "query"). LIKE is most likely used for performing searches from human input, looking for partial matches and being case insensitive. If you know the exact value of your parameter, then you should use = which is also faster.

Also, query() never returns null so it's unnecessary to test for null.

Your code seems correct so if your query returns an empty Cursor it's simply because nothing matches your query in your current database.

Upvotes: 1

Related Questions