[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Test and debug your database\n\nIt's important to verify the stability of your app's database and your users'\ndata when creating databases using the\n[Room persistence library](/training/data-storage/room). This page\ndiscusses how to test your database and perform debugging steps to help your\ntests pass.\n\nTest your database\n------------------\n\nThere are 2 ways to test your database:\n\n- On an Android device.\n- On your host development machine (not recommended).\n\nFor information about testing that's specific to database migrations, see\n[Testing Migrations](/training/data-storage/room/migrating-db-versions#test).\n| **Note:** When running tests for your app, Room allows you to create mock instances of your [DAO](/training/data-storage/room/accessing-data) classes. That way, you don't need to create a full database if you aren't testing the database itself. This functionality is possible because your DAOs don't leak any details of your database.\n\n### Test on an Android device\n\nThe recommended approach for testing your database implementation is writing a\nJUnit test that runs on an Android device. Because these tests don't require\ncreating an activity, they should be faster to execute than your UI tests.\n\nWhen setting up your tests, you should create an in-memory version of your\ndatabase to make your tests more hermetic, as shown in the following example: \n\n### Kotlin\n\n```kotlin\n@RunWith(AndroidJUnit4::class)\nclass SimpleEntityReadWriteTest {\n private lateinit var userDao: UserDao\n private lateinit var db: TestDatabase\n\n @Before\n fun createDb() {\n val context = ApplicationProvider.getApplicationContext\u003cContext\u003e()\n db = Room.inMemoryDatabaseBuilder(\n context, TestDatabase::class.java).build()\n userDao = db.getUserDao()\n }\n\n @After\n @Throws(IOException::class)\n fun closeDb() {\n db.close()\n }\n\n @Test\n @Throws(Exception::class)\n fun writeUserAndReadInList() {\n val user: User = TestUtil.createUser(3).apply {\n setName(\"george\")\n }\n userDao.insert(user)\n val byName = userDao.findUsersByName(\"george\")\n assertThat(byName.get(0), equalTo(user))\n }\n}\n```\n\n### Java\n\n```java\n@RunWith(AndroidJUnit4.class)\npublic class SimpleEntityReadWriteTest {\n private UserDao userDao;\n private TestDatabase db;\n\n @Before\n public void createDb() {\n Context context = ApplicationProvider.getApplicationContext();\n db = Room.inMemoryDatabaseBuilder(context, TestDatabase.class).build();\n userDao = db.getUserDao();\n }\n\n @After\n public void closeDb() throws IOException {\n db.close();\n }\n\n @Test\n public void writeUserAndReadInList() throws Exception {\n User user = TestUtil.createUser(3);\n user.setName(\"george\");\n userDao.insert(user);\n List\u003cUser\u003e byName = userDao.findUsersByName(\"george\");\n assertThat(byName.get(0), equalTo(user));\n }\n}\n```\n\n### Test on your host machine\n\nRoom uses the SQLite Support Library, which provides interfaces that match those\nin the Android Framework classes. This support allows you to pass custom\nimplementations of the support library to test your database queries.\n| **Note:** Even though this setup allows your tests to run very quickly, it isn't recommended because the version of SQLite running on your device---and your users' devices---might not match the version on your host machine.\n\n### Test your migrations\n\nRoom supports [incremental database\nmigrations](/training/data-storage/room/migrating-db-versions) to retain\nexisting app data in situations where an app update changes the database schema.\nHowever, an incorrectly defined migration could cause your app to crash. Make\nsure that you [test your Room database\nmigrations](/training/data-storage/room/migrating-db-versions#test).\n\nDebug your database\n-------------------\n\nThere are several tools and processes that you can use to debug your database.\n\n### Use the Database Inspector\n\nIn Android Studio 4.1 and higher, the Database Inspector allows you to inspect,\nquery, and modify your app's databases while your app is running. The Database\nInspector is compatible with the version of SQLite that is bundled with Android\nand includes special features for use with Room:\n\n- Use gutter actions to quickly run queries from your [DAO\n classes](/training/data-storage/room/accessing-data).\n- Immediately see live updates in the Database Inspector when your running app makes changes to the data.\n\nTo learn more about the Database Inspector, see [Debug your database with the\nDatabase Inspector](/studio/inspect/database).\n\n### Dump data from the command line\n\nThe Android SDK includes a `sqlite3` database tool for examining your app's\ndatabases. It includes commands such as `.dump` to print the contents of a\ntable, and `.schema` to print the `SQL CREATE` statement for an existing table.\n\nYou can also execute SQLite commands from the command line, as shown in the\nfollowing snippet: \n\n```bash\nadb -s emulator-5554 shell\nsqlite3 /data/data/your-app-package/databases/rssitems.db\n```\n\nFor more information, see the [`sqlite3` command line\ndocumentation](http://www.sqlite.org/cli.html), available on the\nSQLite website.\n\nAdditional resources\n--------------------\n\nTo learn more about testing and debugging your Room database, see the following\nadditional resources:\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) (11 Weeks of Android)"]]