binding selectionArgs to a FTS3 query that spans across multiple columns

363 views Asked by At

I construct selection string to perform FTS3 search across multiple columns:

final String selection = "table MATCH 'column1:? OR column2:?'"

I am also passing selectionArgs containing the search terms. The problem is that I'm getting an exception during argument binding:

11-30 13:22:27.475 26281-26323/com.example.myapp W/SuggestionsAdapter﹕ Search suggestions query threw an exception. java.lang.IllegalArgumentException: Cannot bind argument at index 2 because the index is out of range. The statement has 0 parameters. at android.database.sqlite.SQLiteProgram.bind(SQLiteProgram.java:212) at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:166)

When I replace question marks in the MATCH argument string with hard-coded search terms and set selectionArgs = null, the query works and returns correct results.

How can I bind selectionArgs to a FTS3 query that searches across multiple columns?

1

There are 1 answers

0
Y2i On BEST ANSWER

The following approach worked for me:

  • Use String.format() to construct selectionArgs, using search terms as arguments.
  • Use single argument to construct selection

In code:

final String selection = "table MATCH ?"
final String[] selectionArgs = { String.format("column1:%s* OR column2:%s*", term1, term2) };