Android uses the Room database framework

The Room framework is a framework in Jetpack, which simplifies a lot of database operations. Kotlin can add, delete, change and check operations in a few lines of code. The following steps are only for Kotlon

First import kotlin:

// Apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: Implementation 'Androidx. Room: Room-ktx :2.2.1' kapt "Androidx. Room: a room - the compiler: 2.2.1." "Copy the code

Create a bean file

Create userbean. kt class with @entity (tableName = “user”) annotation. TableName indicates the name of the database table

2. Parameters are annotated to @primaryKey, @columnInfo, and @ignore:

@primarykey autoGenerate specifies whether to generate. @columninfo specifies the database name. DefaultValue specifies the defaultValue. Without this annotation, the framework uses variable fields as database fields that the @ignore annotation does not need to be added to the database table

@Entity(tableName = "user")
data class UserBean (

    @PrimaryKey(autoGenerate = true) var id: Int,
    @ColumnInfo(name = "user_name" ,defaultValue = "wzl") var userName: String,
    @ColumnInfo(name = "address") var mAddress: String,
    @Ignore var sex:Int


)
Copy the code

Add Dao files

Dao file is an interface. Add @DAO annotation to indicate that it is used to operate the add, delete, change and check database

@insert Insert database annotation @query Query database annotation, in Query annotation can write operation database code, for example: @query (“select * FROM user WHERE id = :id”); @delete (“select * FROM user WHERE id = :id”)

@Dao interface UserDao { @Insert fun insertUser(user: User): Long @Query("select * FROM user WHERE id = :id") fun queryUser(id: Int):User @Query("select * from user") fun queryAllUser(): Array<User> @Delete fun deleteUser(user: @update fun updateUser(User: User) @update fun updateUser(User: User)Copy the code

Inherit the RoomDatabase class

Entities is an array that contains the type of the Bean you are creating. That is, the Database table you are creating. Version represents the version of the Database

@Database(entities = [User::class,GroupBean::class,BillBean::class,DeviceBean::class,AreaBean::class],version = 1)
abstract class UserDataBase : RoomDatabase() {

}

Copy the code

2. Initialize the DataBase class with a singleton

 companion object{

        private var instance: UserDataBase? = null


        fun getInstance(context: Context): UserDataBase{
            if (instance == null){
                instance = Room.databaseBuilder(
                    context.applicationContext,
                    UserDataBase::class.java,
                    "mUser.db").allowMainThreadQueries().build()

            }
            return instance as UserDataBase
        }

    }
Copy the code

3. Add an interface for the Dao class

	abstract fun userDao() : UserDao
    abstract fun groupDao(): GroupDao
    abstract fun billDao(): BillDao
    abstract fun deviceDao(): DeviceDao
    abstract fun areaDao(): AreaDao
Copy the code

The complete code

@Database(entities = [User::class,GroupBean::class,BillBean::class,DeviceBean::class,AreaBean::class],version = 1)
abstract class UserDataBase : RoomDatabase() {

    abstract fun userDao() : UserDao
    abstract fun groupDao(): GroupDao
    abstract fun billDao(): BillDao
    abstract fun deviceDao(): DeviceDao
    abstract fun areaDao(): AreaDao


    companion object{

        private var instance: UserDataBase? = null


        fun getInstance(context: Context): UserDataBase{
            if (instance == null){
                instance = Room.databaseBuilder(
                    context.applicationContext,
                    UserDataBase::class.java,
                    "mUser.db").allowMainThreadQueries().build()

            }
            return instance as UserDataBase
        }

    }
}
Copy the code

use

It is very simple to use the DataBase singleton activity to the Dao object, and then use the method of add, delete, change and check

val deviceDao = UserDataBase.getInstance(this).deviceDao()
deviceDao.insertDevice(deviceBean)
Copy the code