Android SQLite調試
調試SQLite的神器,再也不用自己去打Log了,只需簡單的幾個命令。 adb shell setprop log.tag.SQLiteLog Vadb shell setprop log.tag.SQLiteStatements Vadb shell stopadb shell start結果如下所示 V/SQLiteStatements( 4405): /data/data/[package]/databases/[db_name].db: "UPDATE [table_name] SET state=-1 WHERE note_id='7556'"想關閉Log也很簡單,把上面代碼中的V改為""就行了 說明在源碼SQLiteDebug.java中 /** * Controls the printing of informational SQL log messages. * * Enable using "adb shell setprop log.tag.SQLiteLog VERBOSE". */ public static final boolean DEBUG_SQL_LOG = Log.isLoggable("SQLiteLog", Log.VERBOSE); /** * Controls the printing of SQL statements as they are executed. * * Enable using "adb shell setprop log.tag.SQLiteStatements VERBOSE". */ public static final boolean DEBUG_SQL_STATEMENTS = Log.isLoggable("SQLiteStatements", Log.VERBOSE); /** * Controls the printing of wall-clock time taken to execute SQL statements * as they are executed. * * Enable using "adb shell setprop log.tag.SQLiteTime VERBOSE". */ public static final boolean DEBUG_SQL_TIME = Log.isLoggable("SQLiteTime", Log.VERBOSE);源碼中說要用VERBOSE,而我們使用V確也能起作用,看下isLoggable的實現就知道了,isLoggable是native方法,實際執行的是frameworks/base/core/jni/android_util_Log.cpp的android_util_Log_isLoggable方法,android_util_Log_isLoggable調用同一檔案內的isLoggable static jboolean isLoggable(const char* tag, jint level) { String8 key; key.append(LOG_NAMESPACE); key.append(tag); char buf[PROPERTY_VALUE_MAX]; if (property_get(key.string(), buf, "") <= 0) { buf[0] = '\0'; } int logLevel = toLevel(buf); return logLevel >= 0 && level >= logLevel;}isLoggable中通過propery_get擷取我們之前設定的值,然後對擷取到的結果調用toLevel static int toLevel(const char* value){ switch (value[0]) { case 'V': return levels.verbose; case 'D': return levels.debug; case 'I': return levels.info; case 'W': return levels.warn; case 'E': return levels.error; case 'A': return levels.assert; case 'S': return -1; // SUPPRESS } return levels.info;}toLevel只判斷了值的第一個字元,所以我們之前只設定V也可以,其實只要是V開頭的,後接任何字元都無所謂。