Android Studio rawQuery with LIKE

26 Views Asked by At
This is my code but no results
String sqlQuery = "WITH Ranked AS (SELECT *, ROW_NUMBER() OVER(PARTITION BY " +
                        COLUMN_NAME + " ORDER BY " + COLUMN_NAME + " ASC, " + COLUMN_SYMBOL + " ASC, " +
                        COLUMN_DTLASTUPD + " DESC) AS rn FROM " + TABLE_RECORDS + ") " +
                        "SELECT * FROM Ranked WHERE rn <= 5 AND " + COLUMN_NAME + " LIKE ? COLLATE NOCASE";
                String[] selectionArgs = new String[]{"%" + "inj" + "%"};

                //String[] selectionArgs = new String[]{whereArgs[0]};
                String finalSqlQuery = String.format(sqlQuery, DatabaseUtils.sqlEscapeString(selectionArgs[0]));
                Log.d("Query Debug", "Final SQL Query: " + finalSqlQuery);
                cursor = db.rawQuery(sqlQuery, selectionArgs);

I was expecting that ? would be replaced by value of selectionArgs.

Final SQL Query: WITH Ranked AS (SELECT *, ROW_NUMBER() OVER(PARTITION BY name ORDER BY name ASC, symbol ASC, dtlastupd DESC) AS rn FROM moedas) SELECT * FROM Ranked WHERE rn <= 5 AND name LIKE ? COLLATE NOCASE

was trhe response, and ? was not replaced with variable given

1

There are 1 best solutions below

1
MikeT On

It is actually via/through calls to the SQLite API that the passed parameters are bound. In your case the parameters will be bound via the SQLite API code underlying the rawQuery method.

So it is SQLite itself that binds the value(s) i.e. replaces the ?(s) with the value properly enclosed i.e. %whatever% passed will be bound/resolved to then be '%whatever%' when the SQL is actually executed.

  • more accurately when the statement is being prepared for execution
  • to determine if the LIKE is the cause of no rows being extracted you could temporarily use .... AND name NOT LIKE ? COLLATE NOCASE, if there are still no results then the issue will not be due to the LIKE clause.