{"id":19672711,"url":"https://github.com/halilozel1903/roomexample","last_synced_at":"2025-07-25T10:33:42.855Z","repository":{"id":91423811,"uuid":"597763790","full_name":"halilozel1903/RoomExample","owner":"halilozel1903","description":"The Room persistence library provides an abstraction layer over SQLite to allow for more robust database access while harnessing the full power of SQLite. 💿 ","archived":false,"fork":false,"pushed_at":"2024-01-01T10:09:37.000Z","size":128,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-27T05:15:48.575Z","etag":null,"topics":["android-database","android-db-manager","coroutines-android","coroutines-room","kotlin-room","room","room-database","room-database-singleton","room-database-test","room-databases","room-db","room-db-crud","roomdatabase","sqlite-android","sqlite-db","sqlite3-database"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/halilozel1903.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-02-05T15:17:07.000Z","updated_at":"2023-02-15T16:40:01.000Z","dependencies_parsed_at":null,"dependency_job_id":"3d811c9a-7f58-45d7-916c-ad1e5dce4db4","html_url":"https://github.com/halilozel1903/RoomExample","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/halilozel1903/RoomExample","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halilozel1903%2FRoomExample","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halilozel1903%2FRoomExample/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halilozel1903%2FRoomExample/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halilozel1903%2FRoomExample/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/halilozel1903","download_url":"https://codeload.github.com/halilozel1903/RoomExample/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halilozel1903%2FRoomExample/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266990998,"owners_count":24017732,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-25T02:00:09.625Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android-database","android-db-manager","coroutines-android","coroutines-room","kotlin-room","room","room-database","room-database-singleton","room-database-test","room-databases","room-db","room-db-crud","roomdatabase","sqlite-android","sqlite-db","sqlite3-database"],"created_at":"2024-11-11T17:13:05.906Z","updated_at":"2025-07-25T10:33:42.804Z","avatar_url":"https://github.com/halilozel1903.png","language":"Kotlin","funding_links":["https://www.buymeacoffee.com/halilozel1903"],"categories":[],"sub_categories":[],"readme":"# Room Database Example 📀 🖥️ 🖱️\n\n![Room Database](https://miro.medium.com/v2/resize:fit:1400/format:webp/1*-OboqZDfl9-XTT8XA-b0cA.png)\n\nRoom is a persistence library, part of the Android Jetpack.\n\nRoom provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.\n\nRoom is now considered as a better approach for data persistence than SQLiteDatabase. It makes it easier to work with SQLiteDatabase objects in your app, decreasing the amount of boilerplate code and verifying SQL queries at compile time.\n\n## Why use Room ⁉️\n- Compile-time verification of SQL queries. each @Query and @Entity is checked at the compile time, that preserves your app from crash issues at runtime and not only it checks the only syntax, but also missing tables.\n- Boilerplate code\n- Easily integrated with other Architecture components (like LiveData)\n\n## Components of Room DB\n\n![Room Database](https://media.geeksforgeeks.org/wp-content/uploads/20210506144355/output.png)\n\nRoom has 3 main components of Room DB :\n- Entity\n- Dao\n- Database\n\n1. `Entity`: Represents a table within the database. Room creates a table for each class that has @Entity annotation, the fields in the class correspond to columns in the table. Therefore, the entity classes tend to be small model classes that don’t contain any logic.\n2. `Dao`: DAOs are responsible for defining the methods that access the database. In the initial SQLite, we use the Cursor objects. With Room, we don’t need all the Cursor related code and can simply define our queries using annotations in the Dao class.\n3. `Database`: Contains the database holder and serves as the main access point for the underlying connection to your app’s persisted, relational data.\nTo create a database we need to define an abstract class that extends RoomDatabase. This class is annotated with @Database, lists the entities contained in the database, and the DAOs which access them.\n\n\n## Entity 🐐\n\n```kotlin\n@Entity(\"person\")\ndata class Person(\n    @PrimaryKey(autoGenerate = true)\n    @ColumnInfo(\"person_id\") @NotNull var person_id: Int,\n    @ColumnInfo(\"person_name\") @NotNull var person_name: String,\n    @ColumnInfo(\"person_age\") @NotNull var person_age: Int\n)\n```\n\n## Dao ⚔️\n\n```kotlin\n@Dao\ninterface PersonDao {\n    @Query(\"SELECT * FROM person\")\n    suspend fun allPerson(): List\u003cPerson\u003e\n\n    @Insert\n    suspend fun insertPerson(person: Person)\n\n    @Update\n    suspend fun updatePerson(person: Person)\n\n    @Delete\n    suspend fun deletePerson(person: Person)\n\n    @Query(\"SELECT * FROM person ORDER BY RANDOM() LIMIT 3\")\n    suspend fun randomPerson(): List\u003cPerson\u003e\n\n    @Query(\"SELECT * FROM person WHERE person_name like '%' || :searchName || '%'\")\n    suspend fun searchPerson(searchName: String): List\u003cPerson\u003e\n\n    @Query(\"SELECT * FROM person WHERE person_id=:person_id\")\n    suspend fun getPerson(person_id: Int): Person\n\n    @Query(\"SELECT count(*) FROM person WHERE person_name=:person_name\")\n    suspend fun controlPerson(person_name: String): Int\n}\n```\n\n# Database 🔥\n\n```kotlin\n@Database(entities = [Person::class], version = 1)\nabstract class PersonDatabase: RoomDatabase() {\n    abstract fun getPersonDao(): PersonDao\n\n    companion object {\n        var INSTANCE: PersonDatabase? = null\n\n        fun accessToDatabase(context: Context): PersonDatabase? {\n            if (INSTANCE == null) {\n                synchronized(PersonDatabase::class) {\n                    INSTANCE = Room.databaseBuilder(\n                        context.applicationContext,\n                        PersonDatabase::class.java,\n                        \"contact.sqlite\"\n                    ).createFromAsset(\"contact.sqlite\").build()\n                }\n            }\n            return INSTANCE\n        }\n    }\n}\n```\n\n# Room Examples ⚖️\n\n```kotlin\nprivate fun getPerson(){\n        CoroutineScope(Dispatchers.Main).launch {\n            val list = personDao.allPerson()\n\n            for (person in list){\n                Log.e(\"Person ID\",person.person_id.toString())\n                Log.e(\"Person Name\", person.person_name)\n                Log.e(\"Person Age\",person.person_age.toString())\n            }\n        }\n    }\n\n    private fun insertPerson(){\n        CoroutineScope(Dispatchers.Main).launch {\n            val newPerson = Person(0,\"Hannah Waddingham\",48)\n            personDao.insertPerson(newPerson)\n        }\n    }\n\n    private fun updatePerson(){\n        CoroutineScope(Dispatchers.Main).launch {\n            val newPerson = Person(12,\"Hilary Duff\",35)\n            personDao.updatePerson(newPerson)\n        }\n    }\n\n    private fun deletePerson(){\n        CoroutineScope(Dispatchers.Main).launch {\n            val deletePerson = Person(13,\"\",0)\n            personDao.deletePerson(deletePerson)\n        }\n    }\n\n    private fun getRandomPerson(){\n        CoroutineScope(Dispatchers.Main).launch {\n            val list = personDao.randomPerson()\n\n            for (person in list){\n                Log.e(\"Person ID\",person.person_id.toString())\n                Log.e(\"Person Name\", person.person_name)\n                Log.e(\"Person Age\",person.person_age.toString())\n            }\n        }\n    }\n\n    private fun searchPerson(){\n        CoroutineScope(Dispatchers.Main).launch {\n            val list = personDao.searchPerson(\"z\")\n\n            for (person in list){\n                Log.e(\"Person ID\",person.person_id.toString())\n                Log.e(\"Person Name\", person.person_name)\n                Log.e(\"Person Age\",person.person_age.toString())\n            }\n        }\n    }\n\n    private fun getOnePerson(){\n        CoroutineScope(Dispatchers.Main).launch {\n            val person = personDao.getPerson(11)\n            Log.e(\"Person ID\", person.person_id.toString())\n            Log.e(\"Person Name\", person.person_name)\n            Log.e(\"Person Age\", person.person_age.toString())\n        }\n    }\n\n    private fun controlPerson(){\n        CoroutineScope(Dispatchers.Main).launch {\n            val size = personDao.controlPerson(\"Taylor Swift\")\n            Log.e(\"Person Size: \", size.toString())\n        }\n    }\n```\n\n## Donation 💸\n\nIf this project help 💁 you, Can you give me a cup of coffee? ☕\n\n[![\"Buy Me A Coffee\"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/halilozel1903)\n\n## License ℹ️\n```\nMIT License\n\nCopyright (c) 2023 Halil OZEL\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhalilozel1903%2Froomexample","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhalilozel1903%2Froomexample","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhalilozel1903%2Froomexample/lists"}