Introduction
API is an application program interface used to develop applications using tools and utilities. API specifies the user interface with the application and to create a user friendly application with the tools and sensor. API has Database, Loader, Frameworks, Contacts.
Database
There are two types in using the databases. One is using the SQLite library and the other is using the room architecture component for storing the data. The latter adds an abstraction layer between the database storage objects. Room is a support architecture component and it should be configured in the platform of Android studio. For configuration, open a module bild.gradle file applies plugin: 'kotlin-kapt'. It is a kotlin compiler plugin that supports annotation processing. Room consists of three objects, Database, Entity, Data Access Object.
Entity
Represent holder for the database. A database contains several entity containers.
Data Access Object
It contains access logic to retrieve data from the database. It serves as an interface between program logic and database model.
Declaration of database
- import android.arch.persistence.room.*
- @Database (entities = arrayOf (Employee :: class, Contact ::class), version = 1)
- abstract class MyDatabase : RoomDatabase()
- {
- abstract fun employeeDao() : EmployeeDao
- abstract fun contactDao() : ContactDao
- abstract fun personDao() : PersonDao
- }
The entity classes are declared inside the @Database annotation. The verion number is used to upgrade diffrent data model versions.
Entity
- @Entity
- data class Employee (@PrimaryKey(autoGenerate = true) var ud:Int = 0.
- var firstName:String,
- var lastName:String)
- @Entity
- data class Contact (@Primarykey(autoGenerate = true) var uid:Int = 0,
- var emailAddr : String)
The primary key of type Int for each entity is required. The column names from a database defined by these entity class match the variable name. The table name is taken from the entity class name.
Nested Object
It is impossible to define inter-object relations other than manually by foreign keys, only by defining a nesting of hierarchical objects.
- @Entity
- data class Employee (
- AprimaryKey (autoGenerate = true) var uid:Int = 0,
- var firstName ::String,
- var lastName ::String)

Join the conversation! Your thoughts help the community grow.