Bagesh Sharma
Bagesh Sharma

Reputation: 889

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

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.

Upvotes: 8

Views: 10037

Answers (3)

Neil MacMillan
Neil MacMillan

Reputation: 208

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.

Upvotes: 8

Bagesh Sharma
Bagesh Sharma

Reputation: 889

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);

Upvotes: 8

user1455310
user1455310

Reputation:

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

Upvotes: 0

Related Questions