Android Room Database Dao Debug Log
Solution 1:
Assuming that Room uses framework's Sqlite as underlying database, the statements can be quite simply logged. The only limitation: this can be done only on emulator.
From SQLiteDebug.java:
/**
* Controls the printing of SQL statements as they are executed.
*
* Enable using "adb shell setprop log.tag.SQLiteStatements VERBOSE".
*/publicstaticfinalboolean DEBUG_SQL_STATEMENTS =
Log.isLoggable("SQLiteStatements", Log.VERBOSE);
By default, the log.tag.SQLiteStatements
's value is not set:
alex@mbpro:~$ adb shell getprop log.tag.SQLiteStatements <-- BLANK LINE -->
According to the above documentation, to set the property we have to use:
alex@mbpro:~$ adb shell setprop log.tag.SQLiteStatements VERBOSE alex@mbpro:~$ adb shell getprop log.tag.SQLiteStatements VERBOSE
As we can see, the VERBOSE
value was successfully set. However, if we'll re-run our application - we won't see those statements printed. To make it work, we'll have to restart all the services using adb shell stop
and then adb shell start
.
If you'll try to do that with a regular device, you'll receive the following error (tried with Pixel XL / stock Android 9):
alex@mbpro:~$ adb shell start start: must be root alex@mbpro:~$ adb root adbd cannot run as root in production builds
This is why we have to use the emulator:
alex@mbpro:~$ adb root restarting adbd as root alex@mbpro:~$ adb shell stop alex@mbpro:~$ adb shell start
The emulator will restart. Run your application and you'll see similar Sqlite statements in logcat:
<redacted..>
V/SQLiteStatements: <redacted>/my_db: "BEGIN EXCLUSIVE;"
V/SQLiteStatements: <redacted>/my_db: "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)"
V/SQLiteStatements: <redacted>/my_db: "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, "3cb5664b6da264c13388292d98141843")"
V/SQLiteStatements: <redacted>/my_db: "CREATE TABLE IF NOT EXISTS `MyTable` (`id` TEXT NOT NULL, `date` INTEGER, `language` TEXT, PRIMARY KEY(`id`))"
<redacted..>
V/SQLiteStatements: <redacted>/my_db: "BEGIN EXCLUSIVE;"
V/SQLiteStatements: <redacted>/my_db: "PRAGMA temp_store = MEMORY;"
V/SQLiteStatements: <redacted>/my_db: "PRAGMA recursive_triggers='ON';"
V/SQLiteStatements: <redacted>/my_db: "CREATE TEMP TABLE room_table_modification_log(version INTEGER PRIMARY KEY AUTOINCREMENT, table_id INTEGER)"
V/SQLiteStatements: <redacted>/my_db: "COMMIT;"
<redacted..>
V/SQLiteStatements: <redacted>/my_db: "SELECT * FROM MyTable"
V/SQLiteStatements: <redacted>/my_db: "SELECT * FROM MyTable WHERE date = 1551562171387 AND language = 'en'"
To undo the changes, use these commands:
alex@mbpro:~$ adb shell setprop log.tag.SQLiteStatements "" alex@mbpro:~$ adb shell getprop log.tag.SQLiteStatements <-- BLANK LINE --> alex@mbpro:~$ adb shell stop alex@mbpro:~$ adb shell start alex@mbpro:~$ adb unroot restarting adbd as non root
Solution 2:
There does not appear to be any hooks for that at the DAO level. There are callbacks related to database opens and upgrades, but not arbitrary stuff.
You could file a feature request, though. I agree that it could be useful. Even better would be an OkHttp-style generic interceptor framework.
Solution 3:
As per document of Room it performs compile time check so if your SQL statement is not valid compilation itself failed and proper error message is displayed in the log.
Also generated code is debuggable by default and can be found under below mentioned path.
build > generated > source > apt > your Package > yourDao_Impl.java
This class contains implementation of your DAO you can debug this class as you debug other classes in your project. :-)
Example :
Solution 4:
As of Room 2.3.0-alpha04 (released December 16 2020, could be stable by the time you're reading this), there's direct support in Room for logging SQL queries with the new RoomDatabase.QueryCallback
You set this callback on the RoomDatabase.Builder
fungetDatabase(context: Context): MyDatabase {
val dbBuilder = Room.databaseBuilder(
context.applicationContext,
MyDatabase::class.java, "mydatabase.db"
)
dbBuilder.setQueryCallback(RoomDatabase.QueryCallback { sqlQuery, bindArgs ->
println("SQL Query: $sqlQuery SQL Args: $bindArgs")
}, Executors.newSingleThreadExecutor())
return dbBuilder.build()
}
Note this is just example code and you should probably ensure MyDatabase
is a singleton in your app. Another tip is only log queries when the app is DEBUG:
if (BuildConfig.DEBUG) dbBuilder.setQueryCallback(
... and the rest of the code from above.
Comment if anybody wants the example code in Java
Solution 5:
When I have got some unknown error while inserting or updating row in room db Android does not show any error in debug console. One thing I found how to check whats happen while debug is:
try { someSource.update(someRow) } catch (e: Throwable) { println(e.message) }
Output is:
UNIQUE constraint failed: quiz.theme (code 2067)
Post a Comment for "Android Room Database Dao Debug Log"