‫Low developers.
‫And welcome back.
‫In this video we're going to start implementing our room database.
‫So we're going to create the three parts the entity the room database and the Dao.
‫I'll start with the entity.
‫An entity refers to a data object that represents a table in the room database.
‫In room, an entity is applying Kotlin class annotated with an entity annotation that defines the schema
‫of a database table.
‫Each instance of this class represents a row in the corresponding table.
‫I'm going to create a table having three columns contact ID, Contact Name, and contact Email with
‫the name contacts table.
‫Let's move to Android Studio and do it in room database.
‫For that, I'll create a new data class here in the package and I'll name it as Contacts or Contact,
‫which is good.
‫Make sure you select Data Class, click enter.
‫Remember, each instance of this class represents a row in the table.
‫Here I need to create three columns.
‫So for that I'll start with val contact underscore id it's of type int.
‫The second one is contact underscore name.
‫It's of type string.
‫And the third column is val contact scored email type string.
‫We created this entity class having the three columns contact ID contact name and contact email.
‫Now let's name our table as contacts underscore table.
‫To do that we're going to use the entity annotation.
‫So add entity.
‫You can see it.
‫We import it from Android X dot room package.
‫And also name the table equals to contacts underscore table.
‫The entity annotation is used to indicate that a class represents a table in the room database.
‫You apply this annotation to the class definition to tell a room database to treat it as a table.
‫A table name attribute within the annotation allows you to specify the name of the table if this attribute
‫is not provided.
‫For example, if I delete it like.
‫This room will use the name of the class as the default table name.
‫So in this case it will name the table as contact, but I prefer adding table names in each table and
‫in each database.
‫We need a primary key to be a unique identifier for each row.
‫For that, I'll use the contact ID as the primary key, and to annotate it, and to tell Android Room
‫Database that this column will act as a primary key.
‫We need to annotate it with a primary key.
‫The primary key annotation is used to designate a property as the primary key for the entity table.
‫You can also specify whether the primary key should be auto generated by room or by using your own values,
‫but I prefer to set it as auto generate equals to true.
‫So if auto generate is set it to true like this case room will automatically assign unique values to
‫the primary key when inserting new records.
‫So in this case, the contact id is the primary key for our table.
‫Another important annotation I'll mention it here is at column info.
‫So for example if I use it here column info.
‫The column info annotation allows you to customize the details of a column in the database table.
‫While it's not mandatory to use column info, you can use it to specify the name of the column if you
‫want it to be different from the property name in the Kotlin class.
‫For example, here I should mention the contact underscore name exactly as the database.
‫Also, I can use it as name equals to contact underscore name.
‫And here I can remove the contact underscore name and set it to name directly.
‫So when fetching the database, everything in the contact underscore name column will be realized as
‫a name in this class.
‫So the database column will be named as contact underscore name instead of name.
‫So these are the columns.
‫But this column will be named as contact name.
‫Since I used the column info annotation.
‫These annotations help room generate the necessary SQL code to generate and create.
‫Eight and manage the database tables based on your Kotlin classes.
‫They provide a way to map Kotlin classes to database tables, and specify important details like the
‫primary keys and the column names.
‫I will not use the column info annotation, but I should mention it if you encountered in other projects
‫and see them in the future.
‫Applications.