Search code examples
androidsqliteloggingadb

How to enable LOGging of all internal SQL cmd's of the Android?


In my learning process I want to focus on next moment: EXACTLY WHICH inside the Android/phone firmware? framework? sys.Libs? SQL commands are being generating in order to process the program code of the end-user's software. Pure example which I MUST to understand and to figured it out right now - HOW, with using of which exactly SQL commands inside the Contact Provider is running the following program code:

Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
ContentResolver resolver = context.getContentResolver();
try (Cursor cursor = resolver.query(uri, PROJECTION, null, null, null)) {
.....
}

Simply speaking: in the Console/Logcat of the Android Studio I want to find out something like that: "SELECT ... FROM phone_lookup INNER JOIN ... ON ... WHERE ... ORDER BY ...." I am using official emulator bundled with latest Android Studio: Pixel_3a_API_30_x86.avd "smartphone" - so it should not even be difficult to use root commands in order to achieve this goal. Right now I can't get it. I've tried next calls:

adb shell setprop log.tag.SQLiteLog VERBOSE
adb shell setprop log.tag.SQLiteStatements VERBOSE
adb shell setprop db.log.slow_query_threshold 0

                   and next:
adb root
adb shell stop
adb shell start

No results. Thus my question is - how and what and where I must use/call/set/create/delete in order to SEE ALL internal SQL commands which are being executed/generated inside the Android's body - inside ALL its internals libraries/plugins/etc. In other words, inside that everything what creates the whole Android's work environment on the phone for the end-user's software.


Solution

  • Ok, as usual - if you want to do something - do making it only by yourself)))

    try (Cursor cursor = resolver.query(uri, PROJECTION, null, null, null))
    

    if in this command line to put in the last argument 100% error string - for example "LIKE bananas" - on the execution stage we will get an error "android.database.sqlite.SQLiteException: near "bananas": syntax error (code 1)" with corresponding staсktrace which will include also and the full SQL command - in which that error was found. Thus we can rather easily to get the knowledge - by WHICH EXACTLY SQL request a particular URI request is being made. And it will work on real phones!!! And so we can easily understand - and why suddenly we do not have an access to a particular data column, which seems to be present in the results of the query(according to online HELP for devs), but for some reason on certain models of phones they do not. And now it becomes easier to understand - and how you can rewrite your request, on the basis of "stacktraced exactly SQL" as it a reference - but with a guarantee of obtaining the necessary data as a result of its execution!