Вопрос

I have a simple database of three columns: title, data, info. I'm storing them in a SQLite FTS3 database so I can easily perform full-text searches. I've been basing my app off the Searchable Dictionary example from Android. The one thing I can't figure out how to do is how to search (match) across two columns. I basically want to take the query and search it in both the title and data columns. The following is the method that prepares the query:

public Cursor searchMatches(String query, String[] columns) {
    String selection = KEY_TITLE + " MATCH ?";
    String[] selectionArguements = new String[] {query + "*"};
    return query(selection, selectionArguements, columns);
}

If I do not follow that exact format KEY_TITLE + " MATCH ?" for the selection, nothing else will work despite the fact that the FTS3 document says other ways can work. How can set up this selection in Android to search across two columns? I've tried adding OR but I simply get a SQL logic error. Thanks a lot!

Это было полезно?

Решение

Alright, I found a workable solution although it isn't the prettiest. It involves using UNION to simply combine two MATCH queries.

    String wordQuery = builder.buildQuery(null, KEY_WORD + " MATCH '" + query + "*'", null, null, null, null, null);
    String definitionQuery = builder.buildQuery(null, KEY_DEFINITION + " MATCH '" + query + "*'", null, null, null, null, null);
    String unionQuery = builder.buildUnionQuery(new String[] {wordQuery, definitionQuery}, null, null);
    Cursor result = helper.getReadableDatabase().rawQuery(unionQuery, null);

Другие советы

You're going to have to write your own query function. You'll want your query to look something like:

 select * from table_name where title equals ? or info equals ?

You can't simply add an or to the equals part of the statement (a common mistake).

This is how I do it

select * from table_name where table_name match 'YOURSEARCHSTRING'
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top