Git Product home page Git Product logo

room-sample's Introduction

Room-Sample

This is a simple example of room library in kotlin.

Add the below dependencies in your app level build.gradle file

apply plugin: 'kotlin-kapt'

dependencies {
implementation "androidx.room:room-runtime:2.2.4"
kapt "androidx.room:room-compiler:2.2.4"
}

Create a Entity class which represents a table in the database

@Entity(tableName = "Employees")
data class Employee(
    @ColumnInfo(name = "emp_name") var name: String,
    @ColumnInfo(name = "emp_salary") var salary:String,
    @ColumnInfo(name = "emp_age") var age:String)
    : Serializable {
    @PrimaryKey(autoGenerate = true) var id: Int = 0
}

Create a DAO interface which contains all queries

@Dao
interface EmployeeDAO {

    @Insert
    fun addEmployee(employee: Employee)

    @Update
    fun updateEmployee(employee: Employee)

    @Query("select * from Employees WHERE id LIKE :emp_id")
    fun getEmployee(emp_id: Int) : Employee

    @Query("select * from Employees")
    fun getAllEmployees() : MutableList<Employee>

    @Delete
    fun deleteEmployee(employee: Employee)

    @Query("DELETE FROM Employees")
    fun deleteAllEmployees()

}

Create a Database class which contains all your DAOs

@Database(entities = [Employee::class],version = 1)
abstract class AppDataBase : RoomDatabase() {

    abstract fun getDAO() : EmployeeDAO

    companion object{
        @Volatile
        private var INSTANCE: AppDataBase? = null

        fun getDatabase(ctx: Context) : AppDataBase{
            val tempInstance = INSTANCE
            if (tempInstance != null) {
                return tempInstance
            }
            synchronized(this){
                val instance = Room.databaseBuilder(
                    ctx.applicationContext,
                    AppDataBase::class.java,
                    "emp_database.db"
                ).allowMainThreadQueries().build()
                INSTANCE = instance
                return instance
            }
        }

    }

}

Usage

val employeeDAO = AppDataBase.getDatabase(this).getDAO()
InsertEmployeeAsyncTask(context,employeeDAO,newEmployee).execute()

class InsertEmployeeAsyncTask(var context: Context, var empDao: EmployeeDAO, var employee: Employee) :
    AsyncTask<Void, Void?, Boolean?>() {

    override fun doInBackground(vararg params: Void?): Boolean {
        empDao.addEmployee(employee)
        return true
    }

    override fun onPostExecute(result: Boolean?) {
        if (result!!) {
            Toast.makeText(context, "Added to Database", Toast.LENGTH_LONG).show()
        }
    }

}

room-sample's People

Contributors

djshah17 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.