In Android Studio 4.1 and higher, the Database Inspector allows you to inspect, query, and modify your app's databases while your app is running. This is especially useful for database debugging. The Database Inspector works with plain SQLite and with libraries built on top of SQLite, such as Room.
Open the Database Inspector
To open a database in the Database Inspector, do the following:
Run your app on an emulator or connected device running API level 26 or higher.
Select View > Tool Windows > Database Inspector from the menu bar.
Select the running app process from the dropdown menu.
The databases in the currently running app appear in the Databases pane. Expand the node for the database that you want to inspect.
View and modify data
The Databases pane displays a list of the databases in your app and the tables that each database contains. Double-click a table name to display its data in the inspector window to the right. You can click a column header to sort the data in the inspector window by that column.
You can modify data in a table by double-clicking a cell, typing a new value,
and pressing Enter. If your app uses Room and your UI observes the database
(such as with
Flow), then any changes you make to the data are
visible in your running app immediately. Otherwise, changes are only visible the
next time your app reads the modified data from the database.
See live database changes
If you want the Database Inspector to automatically update the data it presents as you interact with your running app, check the Live updates checkbox at the top of the inspector window. While live updates are enabled, the table in the inspector window becomes read-only and you cannot modify its values.
Alternatively, you can manually update the data by clicking the Refresh table button at the top of the inspector window.
Query your databases
The Database Inspector can run queries against your app's database while the app is running. The tool can use DAO queries if your app uses Room, but it also supports custom SQL queries.
Run DAO queries
If your app uses Room, Android Studio provides gutter actions that allow you to
quickly run query methods that you have already defined in your DAO
classes. These actions are
available while your app is running and the Database Inspector is open in the
IDE. You can run any query method in a DAO by clicking the Run SQLite
statement in Database Inspector
button next to its
If your app includes more than one database, Android Studio prompts you to select the database to query against from a drop-down list. If your query method includes named bind parameters, Android Studio requests values for each parameter before running the query. The query results are displayed in the inspector window.
Run custom SQL queries
You can also use the Database Inspector to run custom SQL queries against your app's databases while your app is running. To query a database follow these steps:
Click Open New Query tab at the top of the Databases pane to open a new tab in the inspector window.
Figure 3. Open a New Query tab.
If your app includes more than one database, select the database to query from the drop-down list on the New Query tab.
Type your custom SQL query into the text field at the top of the New Query tab and click Run.
Alternatively, you can use the query history feature to run a query that you used previously:
Click the Show query history button to see a list of queries that you previously ran against the selected database.
Figure 4. The query history menu.
Click a query in the list to see a preview of the full query in the editor, and press Enter to copy it to the editor.
Click Run to execute the statement.
The query results that are displayed in the New Query tab are read-only and
can't be modified. However, you can use the custom SQL query field to run
modifier statements such as
DELETE. If your app uses
Room and your UI observes the database (such as with
any changes you make to the data are visible in your running app immediately.
Otherwise, changes are only visible the next time your app reads the modified
data from the database.
In Android Studio 4.2 and higher, you can continue to inspect your app's databases after a process disconnects. This makes it easier to debug your app after a crash.
When a disconnect occurs, the Database Inspector downloads your databases and makes them available to you in offline mode. When offline, you can still open tables and run queries.
When you reconnect to a live app process, the Database Inspector leaves offline mode and shows you only the data that is on the device. In other words, data shown in offline mode does not persist when you reconnect to an app process. Because of this limitation, the Database Inspector doesn't allow you to edit data or run modification SQL statements while in offline mode.
Keep database connections open
The Database Inspector can modify a database only while your app maintains a live connection to that database. That means that if your app frequently connects to and disconnects from databases, it can be difficult to debug those databases. The Databases pane uses icons to identify open () and closed () databases.
Additionally, you can prevent database connections from closing by toggling Keep database connections open from off () to on () at the top of the Databases pane.
To learn more about the Database Inspector, see the following additional resources:
- Database Inspector (11 Weeks of Android)