Domanda

I'm getting results from my rawQuery that duplicate and follow an OR logic as opposed to an AND logic i.e. I will get all the entries that contain "tuxedo" as well as all the entries that contain "hotel" when I only want the ones that contain both.

This is my method:

public ArrayList<Integer> getAdvancedResultIDList(String[] search)
{
    ArrayList<String> queryList = new ArrayList<String>();
    ArrayList<Integer> resultsList = new ArrayList<Integer>();
    SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
    this.mDB = GamesList.mDBHelper.getDatabase();
    Cursor searchCursor = null;
    try
    {
        //TODO:
        // for each string in the search array search the whole of searchable table. Check that the results are only of the values that 
        // contain all the search strings, and add the id of that row to the results ArrayList
        for(int i = 0; i < search.length; i++)
        {
            String query;
            String s = '"' + search[i] + '"';
            query = "SELECT " + KEY_ID + " FROM "+ SEARCHABLE_TABLE + " WHERE " + SEARCHABLE_TABLE + " MATCH " + s;
            queryList.add(query);
        }

        String[] queryArray = queryList.toArray(new String[queryList.size()]);


        String unionQuery = builder.buildUnionQuery(queryArray, KEY_ID + " ASC", null);

        searchCursor = this.mDB.rawQuery(unionQuery, null);

        int colId = searchCursor.getColumnIndex(KEY_ID);

        String resultID;

        for(searchCursor.moveToFirst(); !searchCursor.isAfterLast();searchCursor.moveToNext())
        {
            resultID = searchCursor.getString(colId);
            Integer Id = Integer.parseInt(searchCursor.getString(colId));
            resultsList.add(Id);

        }
        searchCursor.close();

    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        try
        {
            this.mDB.close();

        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    return resultsList;
}

Thanks in advance and Happy New Year!

È stato utile?

Soluzione

The documentation explains how to use multiple search terms:

SELECT id FROM searchTable WHERE searchTable MATCH 'tuxedo hotel'
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top