‫Hello, my friends, and welcome back.
‫In this video, we'll start building our contacts manager application.
‫We'll follow the Mvvm design pattern with room database.
‫So in our local data source, the room database, I'll start designing the entity SQLite, which is
‫the database and the Dao and the contacts Manager Application room database contains only one table,
‫which is the entity named as contacts, table or contacts in this entity.
‫This table I have three columns contact id, which is of type int contact name, which is of type string
‫and the contact email which is of type string.
‫Let's jump into Android studio and start creating our database.
‫And the first step in creating our database is to start with the entity class.
‫To use the room database, we need to add its dependency from the Developer.android.com website.
‫So scroll down, search for Android X room or go to this link developer.android.com slash jetpack slash
‫android slash releases slash room and copy this dependency.
‫These are optional and if you are using the Capt or KSP Kotlin symbol processing, you can use these.
‫I created a new project called Contacts Manager Application in Android Studio and go to build.gradle
‫and paste this dependency the room database Click sync now and I need the annotation processor because
‫all operations like insert, delete, update and et-cetera are annotated also there.
‫As mentioned before, the Add entity Dao database annotations are included, so I need to use the annotation
‫processor.
‫And the annotation processor is a tool that processes annotations at compile time to generate code and
‫perform other tasks.
‫It's often used to automate repetitive code generation and to enforce specific behaviors in your code
‫base.
‫So these annotations provide additional information to the compiler or tools about how to handle the
‫marked elements.
‫For example, in Android you might use annotations like Override, suppress length or custom annotations
‫that you define.
‫Let's start with the first step by creating a new Java class and I'll name it as contacts and to tell
‫Android studio that this class will act as an entity class in our room database.
‫I'll annotated with entity annotation.
‫An entity is a fundamental component that represents a table in the SQLite database.
‫Each entity class corresponds to one table and the fields properties or the variables within the entity
‫class represent INT columns in that table.
‫Entities define the structure and the schema of your database tables.
‫As mentioned before, I have only one entity class called contacts, and I have inside this table three
‫columns, contact ID, contact name and contact email one of type int and two of type strings.
‫And by the way, you can specify various attributes within the entity annotation such as the table name,
‫primary keys and indices.
‫So for example, I'll name this entity or this table table name equal to contacts underscore table.
‫You can specify it like contacts or name it whatever you want.
‫If you don't specify the table name here in the entity, the name of the table is specified exactly
‫as the name of the class.
‫If I remove this table, name the table in our database, but I prefer specifying the table name and
‫I'll name it as contacts underscore table.
‫So now I created this table, this empty table, no columns.
‫I name it as contacts table.
‫The fields within the entity class represent columns in the database table.
‫In this example we have ID name and email fields corresponding to columns in the users table or the
‫contacts underscore table.
‫So for that I'll create private int id, private string name and private string email.
‫Three fields represent three columns in my contacts.
‫Underscore table the name of Collins till now our ID name and email.
‫If I need to change the name of the column you can use at column info.
‫The column info annotation is used.
‫To specify additional details about how a field, property or variable in an entity class maps to a
‫column in the database table.
‫This annotation provides control over column specific properties and behaviors.
‫You can use column info annotation to explicitly specify the name of the database column that corresponds
‫to a field in your entity.
‫For example, this ID if I need to create a database table with the first column contact underscore
‫ID, I can create a field without a column ID like this contact underscore ID and the room database
‫will create a column named as contact ID, or I can specify here the ID as a variable and the column
‫info and I'll specify the name property equal to contacts.
‫Underscore ID.
‫The name of the column will be contact, underscore ID and not ID here.
‫The name of the column is name.
‫I can change it using the same way the column info name equal to contact underscore name.
‫I'm just showing you the power of using the annotations in room and in Android column info.
‫Again guys, you can skip this column info annotation so the room database will create columns using
‫the name of the variable you create in all tables.
‫We need to specify a primary key using a unique and primary key that it will not be repetitive and in
‫this case the contact underscore ID will act as a primary key.
‫So here in the ID field, I'll use an annotation called Primary Key and I'll specify autogenerate equal
‫to true.
‫The primary key annotation indicates that the ID field is the primary key for this table.
‫The autogenerate equal to true attribute means that the SQL will automatically assign unique IDs to
‫the new records.
‫I'll create the constructors Alt+ insert.
‫One having all fields and one having nothing.
‫And this is used to prevent any null pointer exception.
‫I'll create getters and setters.
‫For all elements.