Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid SQLite SELECT WHERE LIKE unexpected behavior
    primarykey
    data
    text
    <p>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.</p> <p>Here is how I build the query:</p> <pre><code>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; } </code></pre> <p>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):</p> <pre><code>Coch**** **** Squi*** St***** Owe** **** Smi** Ca**** G. Bur** Jo****** Gr******* Brown </code></pre> <p>Now when I query "o":</p> <pre><code>Owe** **** G. Bur** Jo****** Gr******* Brown </code></pre> <p>Oddly enough, the first result is filtered out, although it contains an 'o'. When I query "ow":</p> <pre><code>Gr******* Brown </code></pre> <p>And finally when I query with "own":</p> <pre><code>null </code></pre> <p>Here is the method I use to handle the cursor:</p> <pre><code>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(); } </code></pre> <p>Where DBAdapter contains the SQLiteOpenHelper. The context I am passing in comes from a ContentProvider by using the getContext() method.</p> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload