samedi 26 septembre 2015

How to implement Android Sqlite Union rawQuery?

I am using the following code to search the column "English". There is another column called "BANGLA". How I should modify the code so that both "ENGLISH" and "BANGLA" columns are searched simultaneously and returned as combined result?

public List<Bean> getWords(String englishWord) {
        if(englishWord.equals(""))
            return new ArrayList<Bean>();

        String sql = "SELECT * FROM " + TABLE_NAME +
                " WHERE " + ENGLISH + " LIKE ? ORDER BY " + ENGLISH + " LIMIT 100 ";

        SQLiteDatabase db = initializer.getReadableDatabase();

        Cursor cursor = null;
        try {
            cursor = db.rawQuery(sql, new String[]{"%" + englishWord + "%"});

            List<Bean> wordList = new ArrayList<Bean>();
            while(cursor.moveToNext()) {
                int id = cursor.getInt(0);
                String english = cursor.getString(1);
                String bangla = cursor.getString(2);
                String status = cursor.getString(3);
                wordList.add(new Bean(id, english, bangla, status));
            }

            return wordList;
        } catch (SQLiteException exception) {
            exception.printStackTrace();
            return null;
        } finally {
            if (cursor != null)
                cursor.close();
        }
    }

I modifed the String sql to

String sql = "SELECT * FROM " + TABLE_NAME +
                " WHERE " + ENGLISH + " LIKE ? ORDER BY " + ENGLISH + " LIMIT 100 " + " UNION ALL " + " SELECT * FROM " + TABLE_NAME +
                " WHERE " + BANGLA + " LIKE ? ORDER BY " + ENGLISH + " LIMIT 100";

However, the this is not work.

Aucun commentaire:

Enregistrer un commentaire