[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Debug your database with the Database Inspector\n\nThe Database Inspector lets you inspect,\nquery, and modify your app's databases while your app is running. This is\nespecially useful for database debugging. The Database Inspector works with\nplain SQLite and with libraries built on top of SQLite, such as\n[Room](/training/data-storage/room).\n| **Note:** The Database Inspector only works with the SQLite library included in the Android operating system on API level 26 and higher. It doesn't work with other SQLite libraries that you bundle with your app.\n\nOpen the Database Inspector\n---------------------------\n\nTo open a database in the Database Inspector, do the following:\n\n1. [Run your app](/studio/run) on an emulator or connected device running API\n level 26 or higher.\n\n | **Note:** A known issue related to the Android 11 emulator causes apps to crash when connecting to the Database Inspector. To fix the issue, [follow these steps](/studio/known-issues#ki-android-11-db-inspector).\n2. From the menu bar, select **View \\\u003e Tool Windows \\\u003e App Inspection**.\n\n3. Select the **Database Inspector** tab.\n\n4. Select the running app process from the menu.\n\n5. The databases in the currently running app appear in the **Databases**\n pane. Expand the node for the database that you want to inspect.\n\nView and modify data\n--------------------\n\nThe **Databases** pane displays a list of the databases in your app and the\ntables that each database contains. Double-click a table name to display its\ndata in the inspector window to the right, shown in figure 1. Click a column\nheader to sort the data in the inspector window by that column.\n**Figure 1.** The Database Inspector window.\n\nTo modify data in a table, follow these steps:\n\n1. Double-click a cell.\n2. Type a new value.\n3. Press \u003ckbd\u003eEnter\u003c/kbd\u003e.\n\nIf your app uses Room and your UI observes the database, such as with `LiveData`\nor `Flow`, then any changes you make to the data are\nimmediately visible in your running app. Otherwise, changes are only visible the\nnext time your app reads the modified data from the database.\n\n### See live database changes\n\nIf you want the Database Inspector to automatically update the data it presents\nas you interact with your running app, select the **Live updates** checkbox at\nthe top of the inspector window. While live updates are enabled, the table in\nthe inspector window is read-only and you can't modify its values.\n\nAlternatively, to manually update the data, click the **Refresh\ntable** button at the top of the inspector window.\n\nQuery your databases\n--------------------\n\nThe Database Inspector can run queries against your app's database while the app\nis running. The tool can use DAO queries if your app uses Room, but it also\nsupports custom SQL queries.\n\n### Run DAO queries\n\nIf your app uses Room, Android Studio provides gutter actions that let you\nquickly run query methods that you have already defined in your [DAO\nclasses](/training/data-storage/room/accessing-data). These actions are\navailable while your app is running and the Database Inspector is open in the\nIDE.\n\nTo run any query method in a DAO, click the **Run SQLite\nstatement in Database Inspector**\n\nbutton next to its `@Query` annotation.\n**Figure 2.** DAO query gutter actions.\n\nIf your app includes more than one database, Android Studio prompts you to\nselect the database to query against from a list. If your query method\nincludes named [bind\nparameters](/training/data-storage/room/accessing-data#simple-parameters),\nAndroid Studio requests values for each parameter before running the query. The\nquery results are displayed in the inspector window.\n\n### Run custom SQL queries\n\nYou can also use the Database Inspector to run custom SQL queries against your\napp's databases while your app is running.\n\nTo query a database, follow these steps:\n\n1. Click **Open New Query tab**\n at the top of the **Databases** pane to open a new tab\n in the inspector window.\n\n **Figure 3.** Open a **New Query** tab.\n2. If your app includes more than one database, select the database to query\n from the list on the **New Query** tab.\n\n3. At the top of the **New\n Query** tab, type your custom SQL query into the text field.\n\n4. Click **Run**.\n\nAlternatively, use the query history feature to run a query that you\nused previously:\n\n1. Click the\n **Show query history**\n button to see a list of queries that you previously ran against the selected\n database.\n\n **Figure 4.** The query history menu.\n2. Click a query in the list to see a preview of the full query in the\n editor, and press \u003ckbd\u003eEnter\u003c/kbd\u003e to copy it to the editor.\n\n3. Click **Run** to execute the statement.\n\nThe query results that are displayed in the **New Query** tab are read-only and\ncan't be modified. However, you can use the custom SQL query field to run\nmodifier statements such as `UPDATE`, `INSERT`, or `DELETE`.\n\nIf your app uses\nRoom and your UI observes the database, such as with `LiveData` or `Flow`, then\nany changes you make to the data are immediately visible in your running app.\nOtherwise, changes are only visible the next time your app reads the modified\ndata from the database.\n\nOffline mode\n------------\n\nIn Android Studio 4.2 and higher, you can continue to inspect your app's\ndatabases after a process disconnects. This makes it easier to debug your app\nafter a crash.\n\nWhen a disconnect occurs, the Database Inspector downloads your databases and\nmakes them available to you in *offline mode*. When offline, you can still open\ntables and run queries.\n\nWhen you reconnect to a live app process, the Database Inspector leaves offline\nmode and shows you only the data that is on the device. In other words, data\nshown in offline mode does not persist when you reconnect to an app process.\nBecause of this limitation, the Database Inspector doesn't let you edit\ndata or run modification SQL statements while in offline mode.\n\nWhen you are viewing a database in offline mode, the process name includes\n`[DETACHED]` to indicate that the inspector is no longer attached to the\nprocess. Also, the database icon\nindicates the offline state, shown in figure 5.\n**Figure 5.** Database Inspector in offline mode.\n\nKeep database connections open\n------------------------------\n\nThe Database Inspector can modify a database only while your app maintains a\nlive connection to that database. That means that if your app frequently\nconnects to and disconnects from databases, it can be difficult to debug those\ndatabases. The **Databases** pane uses icons to identify open\n\nand closed\n\ndatabases.\n\nAdditionally, to prevent database connections from closing, toggle\n**Keep database connections open** from off\n\nto on\n\nat the top of the **Databases** pane.\n\n#### Export data from the Database Inspector\n\nYou can export databases, tables, and query results from the Database\nInspector to save, share, or re-create locally. When you open up an app project\nin Android Studio and inspect the app for that project in the Database\nInspector, you can start exporting data in one of the following ways:\n\n- Select a database or table in the **Databases** panel and click **Export to\n file** near the top of the panel.\n- Right-click a database or table in the **Databases** panel and select **Export to file** from the context menu.\n- When inspecting a table or query results in a tab, click **Export to file** above the table or query results.\n\nAfter selecting an export action, use the **Export Database** dialog to help you\nthrough the final steps, as shown in figure 6.\n\nDepending on whether you are trying to\nexport a database, table, or query results, you have the option of exporting the\ndata in one or more of the following formats: DB, SQL, or CSV.\n\n\n**Figure 6.** The Export Database dialog.\n\n\u003cbr /\u003e\n\nAdditional resources\n--------------------\n\nTo learn more about the Database Inspector, see the following additional\nresources:\n\n### Blog posts\n\n- [Database Inspector: A live database tool we've been waiting\n for!](https://medium.com/androiddevelopers/database-inspector-9e91aa265316)\n\n### Videos\n\n- [Database Inspector](https://www.youtube.com/watch?v=UMc7Tu0nKYQ)"]]