2

I have an android application with a SQLite database and I am trying to implement a search interface. First of all, I realize that making a FTS database is the way to go, but I thought that I could use a LIKE statement and get similar results if I use wildcards. That way the interface can be tested and the backend updated later.

Here is how I build the query:

public Cursor getMatching(String query) {
    Cursor mCursor = db.query(false, TABLE, new String[] { KEY_ROWID,
            KEY_NAME }, KEY_NAME + " LIKE ?",
            new String[] { "%" + query + "%" }, null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

KEY_NAME is the text column that I want results for, and query is the lowercase string that I want to match against. I would expect that it would return results where the query appears anywhere in the name, case insensitive. However, what I observe is different. The results of querying "" (empty string):

Coch**** ****
Squi*** St*****
Owe** ****
Smi** Ca****
G. Bur** Jo******
Gr******* Brown

Now when I query "o":

Owe** ****
G. Bur** Jo******
Gr******* Brown

Oddly enough, the first result is filtered out, although it contains an 'o'. When I query "ow":

Gr******* Brown

And finally when I query with "own":

null

Here is the method I use to handle the cursor:

public static void logMatches(Context context, String query) {
    DBAdapter adapter = new DBAdapter(context);
    adapter.open();

    Cursor c = adapter.getMatching(query);

    if (c == null)
        return;

    while (c.moveToNext()) {
        String name = c.getString(c
                .getColumnIndex(DBAdapter.KEY_NAME));
        Log.d(TAG, name);
    }

    c.close();
    adapter.close();
}

Where DBAdapter contains the SQLiteOpenHelper. The context I am passing in comes from a ContentProvider by using the getContext() method.

I want to implement a FTS table eventually, but my database is quite small now so I hoped that I could implement similar functionality using existing tables for now. Is it possible to do what I want with the LIKE clause? Am I making an obvious mistake?

skynet
  • 9,898
  • 5
  • 43
  • 52

1 Answers1

1

I did not tried it myself but fts actually implemented in SQLite for android. See for example here and here.

Ah, yes. Instead of while (c.moveToNext()) {} put do { } while (c.moveToNext())

Community
  • 1
  • 1
slkorolev
  • 5,883
  • 1
  • 29
  • 32
  • Like I said, I understand that I should use FTS eventually. However, shouldn't it be possible to use a LIKE statement on the existing database to do what I want, assuming I don't care about performance at the moment? – skynet Oct 23 '11 at 14:40
  • Tried to reproduce your query via Eclipse SQL Scrapbook and failed; that is all the queries return correct results. By the way, as you say your query returns all the rows but one, maybe something's worng with the cursor processing in the code? – slkorolev Oct 23 '11 at 15:49
  • Ah, yes. Instead of 'while (c.moveToNext()) {}' put 'do { } while (c.moveToNext())'. – slkorolev Oct 23 '11 at 16:42
  • That's it! Thank you! Can you edit your answer and I will accept it? – skynet Oct 23 '11 at 16:45