Logging SQL queries in android

后端 未结 9 2261
野趣味
野趣味 2020-12-04 17:45

I am using the query functions in order to build the SQL queries for my tables. Is there a way to see the actual query that is run? For instance log it somewher

相关标签:
9条回答
  • 2020-12-04 18:02
    adb shell setprop log.tag.SQLiteStatements VERBOSE
    

    Don't forget to restart your app after setting this property.

    It is also possible to enable logging of execution time. More details are availabe here: http://androidxref.com/4.2.2_r1/xref/frameworks/base/core/java/android/database/sqlite/SQLiteDebug.java

    0 讨论(0)
  • 2020-12-04 18:02
    adb shell setprop log.tag.SQLiteLog V
    adb shell setprop log.tag.SQLiteStatements V
    adb shell stop
    adb shell start
    0 讨论(0)
  • 2020-12-04 18:07

    You can apply your own SQLiteDatabase.CursorFactory to the database. (See the openDatabase parameters.) This will allow you to create your own subclass of Cursor, which keeps the query in an easily accessible field.

    edit: In fact, you may not even have to subclass Cursor. Just have your factory's newCursor() method return a standard SQLiteCursor, but log the query before doing so.

    0 讨论(0)
  • 2020-12-04 18:09

    Using an SQLiteQueryBuilder it's painfully simple. buildQuery() returns a raw sql string, which can then be logged:

    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
    qb.setTables(ExampleTable.TABLE_NAME);
    String sql = qb.buildQuery(projection, selection, null, null, sortOrder, null);
    Log.d("Example", sql);
    
    0 讨论(0)
  • 2020-12-04 18:09

    If it is for once off scenario, I would suggest injecting an error (e.g. type in expression like LIEK instead of LIKE!) and watch the Eclipse LogCat for any errors! HTH!

    0 讨论(0)
  • 2020-12-04 18:19

    So far the best I could do was to have a look at the cursor's member mQuery using a breakpoint. This member is of course not public and does not have a getter, hence, no way to output it. Any better suggestion?

    0 讨论(0)
提交回复
热议问题