At the initial stage of developing apps with SQLite databases, it was very hard to inspect them and check the queries. Some time later, SQLite browser became the only tool where we could check everything related to app databases, but it was a little bit hard. We needed to export the .db file from the app directory and then open it with the help of SQLite browser to check data in the tables and queries execution. Now we have a few easy tools to inspect the app databases very easily. So why wait? Let’s explore a few options that make debugging the databases easy.
在使用SQLite数据库开发应用程序的初始阶段,很难检查它们并检查查询。 一段时间之后, SQLite浏览器成为我们唯一可以检查与应用程序数据库相关的所有内容的工具,但是有点困难。 我们需要从应用程序目录中导出.db文件,然后在SQLite浏览器的帮助下将其打开以检查表中的数据并查询执行情况。 现在,我们有一些简单的工具可以非常轻松地检查应用程序数据库。 那为什么要等? 让我们探索一些使数据库调试容易的选项。
We will be discussing in detail about Stetho and features of SQL Scout & AppSpector.
我们将详细讨论Stetho和SQL Scout&AppSpector的功能。
Stetho (Stetho)
If you haven’t read my previous article on Stetho, please check it out for an easy understanding. Stetho is a simple library from Facebook with which we can easily debug local storage and network calls. The main question is, “How can we debug the SQLite database using Stetho?”
如果您还没有阅读我以前关于Stetho的文章 ,请阅读一下以方便理解。 Stetho是Facebook提供的一个简单库,通过它我们可以轻松调试本地存储和网络调用。 主要问题是:“如何使用Stetho调试SQLite数据库?”
It’s very easy to use Stetho with only three steps to follow for integration, which provides benefits to inspect the local storage as well as the network layer.
使用Stetho十分容易,只需执行三个步骤即可进行集成,这为检查本地存储以及网络层提供了好处。
步骤1:在build.gradle中添加依赖项 (Step 1: Add dependency in build.gradle)
implementation 'com.facebook.stetho:stetho:1.5.1'// We can also use one of the network helpers to inspect network
// requests
implementation 'com.facebook.stetho:stetho-okhttp3:1.5.1' ORimplementation 'com.facebook.stetho:stetho-urlconnection:1.5.1'
步骤2:使用一行代码在应用程序类中对其进行初始化 (Step 2: Initialize it in the application class with one line of code)
步骤3:启用网络检查 (Step 3: Enable network inspection)
OkHttpClient.Builder()
.addNetworkInterceptor(StethoInterceptor())
.build()
So here the work is done. Simply run the application, and we can easily check the output in the Chrome browser.
这样工作就完成了。 只需运行该应用程序,我们就可以轻松地在Chrome浏览器中检查输出。
Open your Chrome browser and enter chrome://inspect
in the URL bar, hit enter, and then you will be able to see your devices connected as shown in the image below. Click inspect to see the magic!
打开您的Chrome浏览器,然后在URL栏中输入chrome://inspect
,按Enter,然后您将看到连接的设备,如下图所示。 单击检查看魔术!
![Image for post](https://miro.medium.com/max/9999/1*tJPKZXrW2qq43GLZIkgsgw.png)
After clicking the inspect button below the device, a new window will pop up where we can see network fires if we select the Network tab at the top.
单击设备下方的检查按钮后,将弹出一个新窗口,如果选择顶部的“网络”选项卡,则可以看到网络火灾。
使用Stetho进行数据库检查 (Database inspection with Stetho)
When you click on inspect and open the pop-up window, select Resources at the top. Now you will see some options on your left-hand side, from which you can select Web SQL, which is a drop-down where you can see all your app databases. When you click on any table, it will show you the structure with all the data inside it. So simple, right? No need to export and import things as we have done with the SQLite browser.
当您单击检查并打开弹出窗口时,选择顶部的资源。 现在,您会在左侧看到一些选项,从中可以选择Web SQL,这是一个下拉菜单,您可以在其中查看所有应用程序数据库。 当您单击任何表时,它将向您显示其中包含所有数据的结构。 很简单,对吧? 无需像使用SQLite浏览器那样导出和导入内容。
![Image for post](https://miro.medium.com/max/9999/1*6uzCKUhZ0K_SIRRsvJJQsw.png)
We can even execute queries to check the result. Click on the .db file, and you will see an editor on the right-hand side. You can write the queries depending on your requirement to check the results. Below are a few sample queries with results.
我们甚至可以执行查询来检查结果。 单击.db文件,您将在右侧看到一个编辑器。 您可以根据需要编写查询以检查结果。 以下是一些带有结果的示例查询。
![Image for post](https://miro.medium.com/max/9999/1*G6TykQUGbRIqoesFlj6t0w.png)
There is no need to write complex or hard queries directly in the studio, debug them for a long time, and finally discover that there was a mistake in the query rather than other parts. It is a best practice to validate the queries in a prompt as shown above, then copy them to Dao or class wherever they’re required rather than blindly writing queries and getting frustrated while debugging. If you want to explore more, check out Stetho. Now let’s move to SQL Scout.
无需直接在Studio中编写复杂或困难的查询,就可以对其进行长时间的调试,最后发现查询中存在错误而不是其他部分。 最佳实践是在如上所示的提示中验证查询,然后将其复制到Dao或任何需要的类中,而不是盲目地编写查询并在调试时感到沮丧。 如果您想探索更多,请查看Stetho 。 现在,让我们转到SQL Scout。
SQL侦察员 (SQL Scout)
SQLScout is a plug-in for Android Studio and IntelliJ IDEA that provides powerful, dedicated support for managing and debugging SQLite and Room-managed databases.
SQLScout是适用于Android Studio和IntelliJ IDEA的插件,为管理和调试SQLite和Room管理的数据库提供了强大的专用支持。
步骤1 :安装插件 (Step 1: Installing the plug-in)
It’s a plug-in we can install in the Android Studio by following these steps:
我们可以按照以下步骤将其安装在Android Studio中:
- Open the “Settings” (on Windows and Linux) or “Preferences” (on Mac) dialog. 打开“设置”(在Windows和Linux上)或“首选项”(在Mac上)对话框。
- Select “Plug-ins” from the list on the left side. 从左侧列表中选择“插件”。
- Search for SQLScout and then hit the Install button. 搜索SQLScout,然后单击“安装”按钮。
- Restart the studio. 重新启动工作室。
第2步:连接到Android数据库 (Step 2: Connecting to Android Databases)
After the first step, you will see an option of SQLite Explorer on the right-hand side. When you click on it, there will be a pop-up window with an option to explore Databases. When you click on the plus (+) icon, it shows the options to import Databases in multiple ways, either from the device or from local storage. Learn more about how to use each option of connecting to Android Databases.
第一步之后,您将在右侧看到一个SQLite Explorer选项。 当您单击它时,将出现一个弹出窗口,其中包含用于浏览数据库的选项。 当您单击加号(+)图标时,它显示从设备或从本地存储以多种方式导入数据库的选项。 了解有关如何使用每种连接到Android数据库的选项的信息。
![Image for post](https://miro.medium.com/max/9999/1*rAgMJXqSVFWoPuww3P4V4Q.png)
步骤3:在SQLScout中探索的功能 (Step 3: Features to be explored in SQLScout)
SQLite Explorer: Connect to SQLite databases in Android applications or in the file system, browse the elements of a schema, find usages of tables and columns, quickly open in-memory SQL editors, view and edit table data. Learn more.
SQLite Explorer:连接到Android应用程序或文件系统中SQLite数据库,浏览架构元素,查找表和列的用法,快速打开内存中SQL编辑器,查看和编辑表数据。 了解更多 。
![Image for post](https://miro.medium.com/max/9999/1*HhLpwDT2abNBU8Aeqd1LQw.png)
2. SQL Editor: This powerful editor provides a complete set of features that you need from an IDE: syntax highlighting, code completion, navigation to schema and alias definition, and much more. Learn more.
2. SQL编辑器:此功能强大的编辑器提供了IDE所需的一整套功能:语法突出显示,代码完成,导航至模式和别名定义等。 了解更多 。
![Image for post](https://miro.medium.com/max/9999/1*x4TfftfaKLiW7WbfeN_raw.png)
3. Data Console: The data console can display query results, edit table data, and export its content to different formats, such as Excel! Learn more.
3.数据控制台:数据控制台可以显示查询结果,编辑表数据以及将其内容导出为其他格式,例如Excel! 了解更多 。
![Image for post](https://miro.medium.com/max/9999/1*2HMcx0YDOKUWHaWfcmJjtA.png)
4. Database Diagrams: Easily generate diagrams for databases to aid visualization of tables, columns, and relationships. Learn more.
4.数据库图:轻松生成数据库图,以帮助可视化表,列和关系。 了解更多 。
![Image for post](https://miro.medium.com/max/9999/1*osfPG5k0cL0VNG-A1uoXuw.png)
These are the major highlights of SQLScout. It may have some other features too, so explore the complete documentation.
这些是SQLScout的主要亮点。 它还可能具有其他功能,因此请浏览完整的文档 。
AppSpector (AppSpector)
![Image for post](https://miro.medium.com/max/9999/1*1MTytjw47QWxG8ExjUoExQ.png)
AppSpector is a multifunctional remote debugging platform for iOS and Android apps. It is a great solution for any engineering force from solo developers to major corporations. It has many features: We can inspect Network Requests, view application logs, CoreData Browser, SQLite Browser, Track & Mock device location, NSNotificationCenter logs.
AppSpector是适用于iOS和Android应用程序的多功能远程调试平台。 对于从单独开发人员到大型公司的任何工程团队来说,这都是一个很好的解决方案。 它具有许多功能:我们可以检查网络请求,查看应用程序日志,CoreData浏览器,SQLite浏览器,跟踪和模拟设备位置,NSNotificationCenter日志。
主要特点 (Key features)
Remote access
远程访问
You can inspect your app, no matter where it is running: on your phone or a virtual device, in your office or on another continent.
无论应用程序在何处运行,您都可以对其进行检查:在手机或虚拟设备上,在办公室或在其他大陆上。
Customizable
可订制
You choose which data you want to be collected.
您选择要收集的数据。
Easy-to-use
易于使用
You can get started with only a few lines of code and an account on our website.
您仅需几行代码和我们网站上的帐户即可开始使用。
Cross-platform
跨平台
AppSpector offers a similar experience for both iOS and Android users.
AppSpector为iOS和Android用户提供类似的体验。
Real-time
即时的
As soon as anything changes in your app, you will see it in our web interface.
一旦您的应用程序发生任何变化,您就会在我们的Web界面中看到它。
Secure
安全
We will never share your data with anyone else, except for the people you choose.
除了您选择的人之外,我们绝不会与其他任何人共享您的数据。
这个怎么运作 (How it works)
Similar to Stetho, AppSpector consists of two essential parts:
与Stetho相似,AppSpector包含两个基本部分:
1. AppSpector SDKAvailable for both iOS and Android. It is a dependency that can be added to your project. When added, it allows you to collect information about your app state in real time. It is described in detail in the iOS SDK and Android SDK parts, which you can explore on your own.
1. AppSpector SDK适用于iOS和Android。 可以将其添加到您的项目中。 添加后,它允许您实时收集有关您的应用程序状态的信息。 iOS SDK和Android SDK中对此进行了详细说明 零件,您可以自行探索。
2. AppSpector web interfaceAs soon as you launch an application with AppSpector SDK integration, this information becomes available on our website. Every session is completely secure, and its data will not be available to anyone except you and the members of your organization.
2. AppSpector Web界面一旦您启动了与AppSpector SDK集成的应用程序,此信息就会在我们的网站上可用。 每个会话都是完全安全的,除您和组织成员之外,任何人都无法使用其数据。
To explore, more please go through the documentation. It has a 14-day trial period and free plans for small developers.
Please let me know your suggestions and comments.
请让我知道您的建议和意见。
Thanks for reading!
谢谢阅读!
翻译自: https://medium.com/better-programming/android-best-sqlite-debugging-tools-a9a8da07568f