从Spinners构建查询

时间:2014-12-19 20:46:40

标签: java android sqlite

我目前正在开发一个项目,将游戏添加到数据库,然后在textview中显示该游戏及其信息。我有2个微调器,一个用于游戏的第一个字母,另一个用于系统名称。我使用按钮Sort将旋转器分配给字符串,如下所示

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();
                  }

                });

并调用gameData.select()函数

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;
    }

两个旋转器有一个全选择,即显示系统中的所有游戏或所有系统中具有特定字母的所有游戏。我无法获得将要返回的工作查询,例如选择ALL为字母并为系统选择N64,它将显示N64中的所有游戏。游标什么都不返回。

我试过了

                if(letter=="ALL" && system !="ALL")
                    cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME +" WHERE "  +NAME + " = * "+ SYSTEM + " LIKE " + "'" + system + "'",null);
                else if(letter!="ALL" && system =="ALL")
                    cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME +" WHERE "  + NAME + " LIKE " + "'" + letter + "%" + "'" ,null);
                else if (letter=="ALL" && 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;

但没有任何效果。有什么建议吗?

编辑:

现在一切都在使用

 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") && 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);

除了

else if (letter.equals("ALL") && system.equals("ALL"))
                    cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME ,null);

部分内容。它应显示所有系统和字母的所有游戏,但不返回任何内容。

0 个答案:

没有答案