title: Building Queries From Spinners
body: <p>I am currently working on a project that will add a game to a database and then show that game and its information in a textview.  I have 2 spinners, one for the first letter of the game, and one for the name of the system.  I use the button Sort to assign the spinners to a string as shown here </p>
  
  <pre><code>Sort.setOnClickListener(new OnClickListener(){
  
                    @Override
                    //Clears the textview and gets info from the spinners
                    public void onClick(View v) {
                        view.setText("");
                        String system_pick = String.valueOf(system.getSelectedItem());
                        String letter_pick = String.valueOf(letters.getSelectedItem());
                     //displays a toast showing what is being searched for
                        Toast.makeText(SearchActivity.this,
                      "Sorting : " + 
                              "\n Letter: "+ String.valueOf(letters.getSelectedItem()) + 
                              "\n System : "+ String.valueOf(system.getSelectedItem()),
                              Toast.LENGTH_SHORT).show();
                      //Sends a query to the GameData class and returns a cursor  
                      Cursor results = gameData.select( system_pick, letter_pick);
                      results.moveToFirst();
                      //Displays the results in the textview
                      while (results.isAfterLast() == false) {
                          view.append("\n\n" + results.getString(1)+ "\n" + results.getString(2)+ "\n" + results.getInt(3));
                          results.moveToNext();
                      }
                      results.close();
                    }
  
                  });
  </code></pre>
  
  <p>and the gameData.select() function is called which is here</p>
  
  <pre><code>public Cursor select( String system, String letter){
          SQLiteDatabase db = getWritableDatabase();
              Cursor cursor;
  
                  cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME +" WHERE " + NAME + " LIKE " + "'" + letter + "%" + "'" + " AND " + SYSTEM + " LIKE " + "'" + system + "'",null);
              return cursor;
      }
  </code></pre>
  
  <p>The two spinners have an ALL choice i.e display all games in a system or all games with a specific letter in all systems.  I cannot get a query to work that will return, say ALL for letters is selected and N64 is selected for system, it displays all games in N64. the cursor returns nothing. </p>
  
  <p>I have tried </p>
  
  <pre><code>                if(letter=="ALL" &amp;&amp; system !="ALL")
                      cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME +" WHERE "  +NAME + " = * "+ SYSTEM + " LIKE " + "'" + system + "'",null);
                  else if(letter!="ALL" &amp;&amp; system =="ALL")
                      cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME +" WHERE "  + NAME + " LIKE " + "'" + letter + "%" + "'" ,null);
                  else if (letter=="ALL" &amp;&amp; system =="ALL")
                      cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME,null);
                  else
                      cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME +" WHERE " + NAME + " LIKE " + "'" + letter + "%" + "'" + " AND " + SYSTEM + " LIKE " + "'" + system + "'",null);
              return cursor;
  </code></pre>
  
  <p>but nothing worked.  Any advice?</p>
  
  <p>EDIT:</p>
  
  <p>Everything is now working using </p>
  
  <pre><code> if(letter.equals("ALL"))
                      cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME +" WHERE " + SYSTEM + " LIKE " + "'" + system + "'",null);
                  else if(system.equals("ALL"))
                      cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME +" WHERE "  + NAME + " LIKE " + "'" + letter + "%" + "'" ,null);
                  else if (letter.equals("ALL") &amp;&amp; system.equals("ALL"))
                      cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME ,null);
                  else
                      cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME +" WHERE " + NAME + " LIKE " + "'" + letter + "%" + "'" + " AND " + SYSTEM + " LIKE " + "'" + system + "'",null);
  </code></pre>
  
  <p>except for the </p>
  
  <pre><code>else if (letter.equals("ALL") &amp;&amp; system.equals("ALL"))
                      cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME ,null);
  </code></pre>
  
  <p>part of this.  It should display all games from all systems and letters but returns nothing.</p>