تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
من المهم التأكّد من ثبات قاعدة بيانات تطبيقك واستقرار عمل المستخدمين
البيانات عند إنشاء قواعد البيانات باستخدام
مكتبة العناصر الثابتة للغرفة. هذه الصفحة
كيفية اختبار قاعدة البيانات وإجراء خطوات تصحيح الأخطاء لمساعدة
اجتياز الاختبارات.
اختبار قاعدة البيانات
هناك طريقتان لاختبار قاعدة البيانات:
على جهاز Android
على جهاز التطوير المضيف (لا يُنصح بهذا الإجراء).
للحصول على معلومات عن الاختبارات الخاصة بعمليات نقل بيانات قاعدة البيانات، راجِع
اختبار عمليات نقل البيانات:
الاختبار على جهاز Android
النهج الموصى به لاختبار تنفيذ قاعدة البيانات هو كتابة
اختبار JUnit الذي يتم إجراؤه على جهاز Android. لأن هذه الاختبارات لا تتطلب
أثناء إنشاء نشاط، فينبغي أن تكون أسرع في التنفيذ من اختبارات واجهة المستخدم الخاصة بك.
عند إعداد الاختبارات، يجب عليك إنشاء نسخة مخزنة في الذاكرة من
لجعل اختباراتك أكثر غموضًا، كما هو موضح في المثال التالي:
تستخدم الغرفة مكتبة دعم SQLite، والتي توفر واجهات تتوافق مع تلك
في فئات إطار عمل Android. يسمح لك هذا الدعم باجتياز
وتطبيقات مكتبة الدعم لاختبار استعلامات قاعدة البيانات.
اختبار عمليات نقل البيانات
تتوافق الغرفة مع قاعدة بيانات تزايدية
لنقل البيانات للاحتفاظ
بيانات التطبيق الحالية في الحالات التي يغيّر فيها تحديث التطبيق مخطط قاعدة البيانات.
ومع ذلك، يمكن أن تؤدي عملية نقل البيانات المحدَّدة بشكل غير صحيح إلى تعطُّل تطبيقك. الماركة
من اختبار قاعدة بيانات الغرفة
.
تصحيح أخطاء قاعدة البيانات
هناك العديد من الأدوات والعمليات التي يمكنك استخدامها لتصحيح أخطاء قاعدة البيانات.
استخدام عارض قاعدة البيانات
وفي Android Studio 4.1 والإصدارات الأحدث، يسمح لك Database Inspector (أداة فحص قاعدة البيانات) بفحص
في قواعد بيانات التطبيق
وتعديلها أثناء تشغيله. قاعدة البيانات
يتوافق Inspector مع إصدار SQLite المضمّن مع Android.
وتشمل ميزات خاصة لاستخدامها مع الغرفة:
استخدام إجراءات هامش التوثيق لتشغيل طلبات البحث بسرعة من DAO
الصفوف.
الاطّلاع على التحديثات المباشرة فورًا في Database Inspector (أداة فحص قاعدة البيانات) عند تشغيل تطبيقك
تُجري تغييرات على البيانات.
تتضمّن حزمة تطوير البرامج (SDK) لنظام التشغيل Android أداة قاعدة بيانات sqlite3 لفحص بيانات تطبيقك.
لقواعد البيانات. وهو يتضمن أوامر مثل .dump لطباعة محتوى
و.schema لطباعة عبارة SQL CREATE لجدول موجود.
يمكنك أيضًا تنفيذ أوامر SQLite من سطر الأوامر، كما هو موضح في
المقتطف التالي:
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ Java وOpenJDK هما علامتان تجاريتان مسجَّلتان لشركة Oracle و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 2025-07-27 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","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,["# 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)"]]