Android Studio的数据库检查器是否要求您在显示数据库架构之前查询数据库?

huangapple go评论55阅读模式
英文:

Does the database inspector in Android Studio require you to query the database before it shows the schema of a database?

问题

我正在开发一个简单的应用程序,其中包含一个我从其他地方获取的数据库文件。我无法在数据库检查器中看到数据库。

我通过使用VSCode检查数据库是否为空,但它并不为空。最后,我在存储库和DAO中添加了获取数据库中所有行的功能。然后我添加了以下代码行

在UI状态中(我应用程序中称为searchState)

val allAirports: List<Airport> = emptyList()

在ViewModel中

init {
    getAllAirports()
}

fun getAllAirports() {
    viewModelScope.launch {
        searchState = searchState.copy(allAirports = airportRepository.getAllAirports() )
    }
}

结果,数据库出现在数据库检查器中。

为了测试是否查询数据库导致模式显示出来,我删除了init{}块。当我再次运行应用程序时,数据库显示出来,但表格没有显示。此外,数据库标记为“closed”。

英文:

I am working on a simple app with a database file I got from somewhere else. I couldn't get the database to show up on the database inspector.

I checked to see if the database was empty by using VSCode but it wasn't empty. Finally I added the functionality to fetch all rows in the database in the repository and the DAO. And then I added these lines of code

in the UI State (called searchState in my app)

val allAirports: List&lt;Airport&gt; = emptyList()

in the View Model

init {
    getAllAirports()
}

fun getAllAirports() {
    viewModelScope.launch {
        searchState = searchState.copy(allAirports = airportRepository.getAllAirports() )
    }
}

And what do you know, the database showed up on the database inspector.

To test if querying the database is what caused the schema to show, I removed the init{} block. When I ran the app again, the database showed but the tables did not. Furthermore, the database was marked as "closed"

答案1

得分: 1

你只能在数据库检查器打开时查看数据库中的表数据。所以,我所做的是,在数据库打开后(在代码中),在它关闭之前(在代码中),我添加了一个断点。我可以在数据检查器中看到表格中的数据。

但需要注意的重要一点是:在以调试模式运行应用程序之前,请确保数据库检查器选项卡已打开。

英文:

You can view the table data in the database inspector while the database is OPEN only. So, what I did was, I added a breakpoint for a line of code after the database is open (in the code) and before it's closed (in the code). I could see the data in the tables in the data inspector.

But an important thing to note is: before you run the app in the debug mode, have the database inspector tab open.

huangapple
  • 本文由 发表于 2023年4月20日 01:55:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057523.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定