Home » Android » sqlite – How to print query executed by query() method of android

sqlite – How to print query executed by query() method of android

Posted by: admin June 15, 2020 Leave a comment

Questions:

We pass so many parameters to query() method of android. Android has simplified our work by giving query method. Is there any way so that i can print the sql query which android will form from this query method arguments which android creates and sends to sqlite.

How to&Answers:

According to a previous post, I have tried and I got the following solution in order to print the query string in the log.

Use buildQueryString method of SQLiteQueryBuilder. It takes almost same parameters as query() method takes ………

String sqlQry = SQLiteQueryBuilder.buildQueryString(false,TABLE_NAME, null, COLUMN_NAME_PATIENTID +"="+ patientID, null, null, COLUMN_NAME_PATIENTFIRSTNAME, null);

Log.i(TAG, sqlQry);

cursor = db.query(TABLE_NAME, null, COLUMN_NAME_PATIENTID +"="+ patientID, null, null, null, COLUMN_NAME_PATIENTFIRSTNAME);

Answer:

For what it’s worth, if you run under the debugger you can view the private member variable mQuery, which shows you the exact SQL query executed on that cursor – it’s handy and can be used on demand without mucking with any code.

Answer:

Since the query methods are of cursor type, I am not sure whether It will be printed or not.
If you want to debug any query, you can use EXPLAIN QUERY PLAN keyword along with the query or use SQLiteQueryBuilder() or simply run the SQL query by using rawQuery() method.
You can take references from:

http://developer.android.com/reference/android/database/sqlite/SQLiteQueryBuilder.html

http://www.sqlite.org/eqp.html