Search code examples
androidsqliteviewcursorprojection

View final query in android


Cursor c = cr.query(data_uri, projection, where, selectionArgs, ContactsContract.Data.DISPLAY_NAME + " COLLATE LOCALIZED ASC");

When the where clause contains a ? in it the data is picked up from selectionArgs so is there a way to see the final query before it is executed by the system?

Android gives the final query in the logcat when there is an error in the system but is there a way to get it otherwise?


Solution

  • Yes and no.

    Under the hood many providers use SQLiteQueryBuilder to construct the SQL. You can use it to construct a SQL string and then to log it.

    ? variables are part of the SQL itself. Binding values to them happens after the SQL is compiled. You cannot get a "query string" with the variables replaced with corresponding literals, but you can log your selectionArgs separately.