تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
توفر مكتبة بقاء الغرفة عددًا من الفوائد مقارنة باستخدام SQLite
واجهات برمجة التطبيقات مباشرةً:
التحقق من وقت التجميع لطلبات بحث SQL
تعليقات توضيحية ملائمة تقلّل من النصوص النموذجية المتكرّرة والمعرَّضة للأخطاء
رقم الاعتماد
مسارات نقل بيانات مبسّطة لقواعد البيانات
إذا كان تطبيقك يستخدم حاليًا تنفيذ SQLite خارج الغرفة، يُرجى الاطّلاع على هذه الصفحة.
للتعرّف على كيفية نقل تطبيقك لاستخدام الغرفة بدلاً من ذلك. إذا كانت الغرفة هي أول
تنفيذ SQLite الذي تستخدمه في تطبيقك، يُرجى الاطّلاع على حفظ البيانات في دليل محلي
باستخدام الغرفة للحصول على معلومات الاستخدام الأساسية.
خطوات نقل البيانات
نفِّذ الخطوات التالية لنقل تنفيذ SQLite إلى الغرفة. في حال حذف
تنفيذ SQLite يستخدم قاعدة بيانات كبيرة أو استعلامات معقدة، يمكنك
يفضّلون النقل إلى الغرفة تدريجيًا. يُرجى الاطّلاع على النقل المتزايد
للاستفادة من استراتيجية نقل بيانات إضافية
تعديل الاعتماديات
لاستخدام الغرفة في تطبيقك، يجب تضمين العناصر الاعتمادية المناسبة في
الملف build.gradle للتطبيق. راجع الإعداد لـ
وأحدث تبعيات الغرفة.
تعديل فئات النماذج في كيانات البيانات
تستخدم الغرفة كيانات البيانات من أجل
لتمثيل الجداول في قاعدة البيانات. تمثل كل فئة كيان جدولاً
يحتوي على حقول تمثل الأعمدة في هذا الجدول. يُرجى اتّباع هذه الخطوات لتعديل
فئات النماذج الحالية لتكون وحدات الغرف:
ضع تعليقًا توضيحيًا على إعلان الفئة باستخدام
@Entity للإشارة إلى أنّه
كيان الغرفة. يمكنك اختياريًا استخدام
tableName
الإشارة إلى أن الجدول الناتج يجب أن يكون له اسم مختلف عن
اسم الفئة.
قم بإضافة تعليق توضيحي لحقل المفتاح الأساسي
@PrimaryKey
إذا كان يجب أن يكون لأي من الأعمدة في الجدول الناتج اسم
مختلف عن اسم الحقل المقابل، فقم بإضافة تعليقات توضيحية إلى الحقل
@ColumnInfo وضبط
name على
باسم العمود الصحيح.
إذا كانت الفئة تحتوي على حقول لا تريد الاحتفاظ بها في قاعدة البيانات،
إضافة ملاحظات توضيحية إلى هذه الحقول
@Ignore للإشارة إلى تلك الغرفة
يجب ألا تنشئ أعمدة لها في الجدول المقابل.
إذا كانت الفئة تحتوي على أكثر من طريقة إنشائية واحدة، فوضح أي دالة إنشائية
يجب استخدام الغرفة من خلال إضافة تعليقات توضيحية إلى جميع التركيبات الإنشائية الأخرى باستخدام @Ignore.
تستخدم الغرفة عناصر الوصول إلى البيانات (DAO) لتحديد الطرق التي يمكنها الوصول إلى قاعدة البيانات.
اتّبِع الإرشادات الواردة في مقالة الوصول إلى البيانات باستخدام الغرفة.
رموز DAO لاستبدال طلب البحث الحالي
باستخدام DAOs.
إنشاء فئة قاعدة بيانات
تستخدم عمليات تنفيذ الغرفة فئة قاعدة بيانات لإدارة نسخة افتراضية من قاعدة بيانات
قاعدة البيانات. يجب تمديد فئة قاعدة البيانات
RoomDatabase والإشارة إلى الكل
للكيانات وأنظمة DAO التي حددتها.
نظرًا لأن رقم إصدار قاعدة البيانات يتغير، يجب عليك تحديد
Migration من أجل
تشير إلى مسار نقل بيانات بحيث تحتفظ الغرفة بالبيانات الموجودة في قاعدة البيانات.
طالما أن مخطط قاعدة البيانات لا يتغير، يمكن أن يكون هذا الحقل فارغًا
التنفيذ.
Kotlin
valMIGRATION_1_2=object:Migration(1,2){overridefunmigrate(database:SupportSQLiteDatabase){// Empty implementation, because the schema isn't changing.}}
Java
staticfinalMigrationMIGRATION_1_2=newMigration(1,2){@Overridepublicvoidmigrate(SupportSQLiteDatabasedatabase){// Empty implementation, because the schema isn't changing.}};
لمزيد من المعلومات حول مسارات نقل قاعدة البيانات في الغرفة، يُرجى الاطّلاع على المقالة نقل بيانات
قاعدة بيانات.
تحديث مثيل قاعدة البيانات
بعد تحديد فئة قاعدة بيانات ومسار نقل البيانات، يمكنك استخدام
Room.databaseBuilder
لإنشاء مثيل من قاعدة البيانات مع تطبيق مسار نقل البيانات:
اتّبِع الإرشادات الواردة في المقالة اختبار.
نقل البيانات لاختبارها
وترحيل قاعدة البيانات.
اتّبِع الإرشادات الواردة في المقالة اختبار
لاختبار DAO
الطرق.
نقل البيانات المتزايد
إذا كان تطبيقك يستخدم قاعدة بيانات كبيرة ومعقدة، قد لا يكون من الممكن نقل البيانات.
نقل تطبيقك إلى الغرفة في آنٍ واحد. بدلاً من ذلك، يمكنك تنفيذ البيانات
والكيانات وقاعدة بيانات الغرف كخطوة أولى، ثم نقل طرق الاستعلام
إلى DAO لاحقًا. ويمكنك إجراء ذلك من خلال استبدال مساعد قاعدة البيانات المخصّص.
الفئة مع
SupportSQLiteOpenHelper
العنصر الذي تتلقاه من
RoomDatabase.getOpenHelper()
مصادر إضافية
لمعرفة المزيد من المعلومات حول نقل البيانات من SQLite إلى Room، يمكنك الاطّلاع على العناصر الإضافية التالية
الموارد:
يخضع كل من المحتوى وعيّنات التعليمات البرمجية في هذه الصفحة للتراخيص الموضحّة في ترخيص استخدام المحتوى. إنّ 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,["# Migrate from SQLite to Room\n\nThe Room persistence library provides a number of benefits over using the SQLite\nAPIs directly:\n\n- Compile-time verification of SQL queries\n- Convenience annotations that minimize repetitive and error-prone boilerplate code\n- Streamlined database migration paths\n\nIf your app currently uses a non-Room implementation of SQLite, read this page\nto learn how to migrate your app to use Room instead. If Room is the first\nSQLite implementation that you are using in your app, see [Save data in a local\ndatabase using Room](/training/data-storage/room) for basic usage information.\n\nMigration steps\n---------------\n\nPerform the following steps to migrate your SQLite implementation to Room. If\nyour SQLite implementation uses a large database or complex queries, you might\nprefer to migrate to Room gradually. See [Incremental migration](#incremental)\nfor an incremental migration strategy.\n\n### Update dependencies\n\nTo use Room in your app, you must include the appropriate dependencies in your\napp's `build.gradle` file. See [Setup](/training/data-storage/room#setup) for\nthe most up-to-date Room dependencies.\n\n### Update model classes to data entities\n\nRoom uses [data entities](/training/data-storage/room/defining-data) to\nrepresent the tables in the database. Each entity class represents a table and\nhas fields that represent columns in that table. Follow these steps to update\nyour existing model classes to be Room entities:\n\n1. Annotate the class declaration with [`@Entity`](/reference/kotlin/androidx/room/Entity) to indicate that it is a Room entity. You can optionally use the [`tableName`](/reference/kotlin/androidx/room/Entity#tablename) property to indicate that the resulting table should have a name that is different from the class name.\n2. Annotate the primary key field with [`@PrimaryKey`](/reference/kotlin/androidx/room/PrimaryKey).\n3. If any of the columns in the resulting table should have a name that is different from the name of the corresponding field, annotate the field with [`@ColumnInfo`](/reference/kotlin/androidx/room/ColumnInfo) and set the [`name`](/reference/kotlin/androidx/room/ColumnInfo#name) property to the correct column name.\n4. If the class has fields that you do not want to persist in the database, annotate those fields with [`@Ignore`](/reference/kotlin/androidx/room/Ignore) to indicate that Room should not create columns for them in the corresponding table.\n5. If the class has more than one constructor method, indicate which constructor Room should use by annotating all of the other constructors with `@Ignore`.\n\n### Kotlin\n\n```kotlin\n@Entity(tableName = \"users\")\ndata class User(\n @PrimaryKey\n @ColumnInfo(name = \"userid\") val mId: String,\n @ColumnInfo(name = \"username\") val mUserName: String?,\n @ColumnInfo(name = \"last_update\") val mDate: Date?,\n)\n```\n\n### Java\n\n```java\n@Entity(tableName = \"users\")\npublic class User {\n\n @PrimaryKey\n @ColumnInfo(name = \"userid\")\n private String mId;\n\n @ColumnInfo(name = \"username\")\n private String mUserName;\n\n @ColumnInfo(name = \"last_update\")\n private Date mDate;\n\n @Ignore\n public User(String userName) {\n mId = UUID.randomUUID().toString();\n mUserName = userName;\n mDate = new Date(System.currentTimeMillis());\n }\n\n public User(String id, String userName, Date date) {\n this.mId = id;\n this.mUserName = userName;\n this.mDate = date;\n }\n\n}\n```\n\n### Create DAOs\n\nRoom uses data access objects (DAOs) to define methods that access the database.\nFollow the guidance in [Accessing data using Room\nDAOs](/training/data-storage/room/accessing-data) to replace your existing query\nmethods with DAOs.\n\n### Create a database class\n\nImplementations of Room use a database class to manage an instance of the\ndatabase. Your database class should extend\n[`RoomDatabase`](/reference/kotlin/androidx/room/RoomDatabase) and reference all\nof the entities and DAOs that you have defined.\n**Note:** The migration to Room represents a SQLite database version change, so make sure that you increment the version number by one. \n\n### Kotlin\n\n```kotlin\n@Database(entities = [User::class], version = 2)\n@TypeConverters(DateConverter::class)\nabstract class UsersDatabase : RoomDatabase() {\n abstract fun userDao(): UserDao\n}\n```\n\n### Java\n\n```java\n@Database(entities = {User.class}, version = 2)\n@TypeConverters(DateConverter.class)\npublic abstract class UsersDatabase extends RoomDatabase {\n public abstract UserDao userDao();\n}\n```\n\n### Define a migration path\n\nSince the database version number is changing, you must define a\n[`Migration`](/reference/kotlin/androidx/room/migration/Migration) object to\nindicate a migration path so that Room keeps the existing data in the database.\nAs long as the database schema does not change, this can be an empty\nimplementation. \n\n### Kotlin\n\n```kotlin\nval MIGRATION_1_2 = object : Migration(1, 2) {\n override fun migrate(database: SupportSQLiteDatabase) {\n // Empty implementation, because the schema isn't changing.\n }\n}\n```\n\n### Java\n\n```java\nstatic final Migration MIGRATION_1_2 = new Migration(1, 2) {\n @Override\n public void migrate(SupportSQLiteDatabase database) {\n // Empty implementation, because the schema isn't changing.\n }\n};\n```\n\nTo learn more about database migration paths in Room, see [Migrate your\ndatabase](/training/data-storage/room/migrating-db-versions).\n\n### Update the database instantiation\n\nAfter you have defined a database class and a migration path, you can use\n[`Room.databaseBuilder`](/reference/kotlin/androidx/room/Room#databasebuilder)\nto create an instance of your database with the migration path applied: \n\n### Kotlin\n\n```kotlin\nval db = Room.databaseBuilder(\n applicationContext,\n AppDatabase::class.java, \"database-name\"\n )\n .addMigrations(MIGRATION_1_2).build()\n```\n\n### Java\n\n```java\ndb = Room.databaseBuilder(\n context.getApplicationContext(),\n UsersDatabase.class, \"database-name\"\n )\n .addMigrations(MIGRATION_1_2).build();\n```\n\n### Test your implementation\n\nMake sure you test your new Room implementation:\n\n- Follow the guidance in [Test\n migrations](/training/data-storage/room/migrating-db-versions#test) to test your database migration.\n- Follow the guidance in [Test your\n database](/training/data-storage/room/testing-db#test) to test your DAO methods.\n\nIncremental migration\n---------------------\n\nIf your app uses a large, complex database, it might not be feasible to migrate\nyour app to Room all at once. Instead, you can optionally implement the data\nentities and Room database as a first step and then migrate your query methods\ninto DAOs later. You can do this by replacing your custom [database helper\nclass](/training/data-storage/sqlite#DbHelper) with the\n[`SupportSQLiteOpenHelper`](/reference/kotlin/androidx/sqlite/db/SupportSQLiteOpenHelper)\nobject that you receive from\n[`RoomDatabase.getOpenHelper()`](/reference/kotlin/androidx/room/RoomDatabase#getopenhelper).\n\nAdditional resources\n--------------------\n\nTo learn more about migrating from SQLite to Room, see the following additional\nresources:\n\n### Blogs\n\n- [7 Steps to Room](https://medium.com/androiddevelopers/7-steps-to-room-27a5fe5f99b2)\n- [Incrementally migrate from SQLite to Room](https://medium.com/androiddevelopers/incrementally-migrate-from-sqlite-to-room-66c2f655b377)"]]