Android Room Database DAO调试日志 [英] Android Room Database DAO debug log

查看:369
本文介绍了Android Room Database DAO调试日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个Room数据库DAO,如下所示:

Given a Room database DAO like this:

import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Query;

import java.util.Date;
import java.util.List;

@Dao
public interface MyDao {

    @Query("SELECT * FROM MyTable")
    List<MyItem> all();

    @Query("SELECT * FROM MyTable WHERE date = :date AND language = :language")
    MyItem byDate(Date date, String language);


}

有没有一种方法可以将Logger或类似的东西添加到MyDao中,以便我可以看到正在执行的语句.这将在开发过程中非常有用,因为我可以立即检查函数是否正确转换为预期的SQL语句.

Is there a way to have a Logger or something like that added to MyDao so that I could see which statements are being performed. This would be really helpful during development, because I could immediately check if the functions are transformed correctly to the expected SQL statement or not.

推荐答案

假定Room使用框架的Sqlite作为基础数据库,则可以很简单地记录语句.唯一的限制:只能在模拟器上完成此操作.

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.

来自 SQLiteDebug .java :

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


默认情况下,未设置log.tag.SQLiteStatements的值:


By default, the log.tag.SQLiteStatements's value is not set:

alex @ mbpro:〜$ adb shell getprop log.tag.SQLiteStatements
<-空白行->

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
详细

alex@mbpro:~$ adb shell setprop log.tag.SQLiteStatements VERBOSE
alex@mbpro:~$ adb shell getprop log.tag.SQLiteStatements
VERBOSE

如我们所见,VERBOSE值已成功设置.但是,如果我们重新运行我们的应用程序-我们将看不到这些语句的打印.要使其正常工作,我们必须先使用adb shell stop然后adb shell start 重新启动所有服务. > 如果您尝试使用常规设备执行此操作,则会收到以下错误(已在Pixel XL/Android 9上尝试过):

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启动
开始:必须是root
alex @ mbpro:〜$ adb根
adbd无法在生产版本中以root用户身份运行

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身份重新启动adbd
alex @ mbpro:〜$ adb shell停止
alex @ mbpro:〜$ adb shell启动

alex@mbpro:~$ adb root
restarting adbd as root
alex@mbpro:~$ adb shell stop
alex@mbpro:~$ adb shell start

模拟器将重新启动.
运行您的应用程序,您将在logcat中看到类似的Sqlite语句:

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
<-空白行->
alex @ mbpro:〜$ adb shell停止
alex @ mbpro:〜$ adb shell启动
alex @ mbpro:〜$ adb取消root
以非root身份重新启动adbd

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

这篇关于Android Room Database DAO调试日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆