Home > Enterprise >  How do i make multiple tables in room
How do i make multiple tables in room

Time:11-02

I have 1 entity and i am trying to add another 6 but cant do it.

@Database(entities = [StaffTable::class] , version = 1, exportSchema = false)
abstract class HarryPotterDatabase: RoomDatabase() {

    abstract  fun harryPotterDao(): HarryPotterDao

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

        fun getDatabase(context: Context): HarryPotterDatabase{
            val tempInstance = INSTANCE
            if (tempInstance != null){
                return tempInstance
            }
            synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    HarryPotterDatabase::class.java,
                    "user_database"
                ).build()

                INSTANCE = instance
                return instance
            }
        }
    }
}

CodePudding user response:

@Database(
    entities = [UserInfo::class, Transaction::class, Loan::class,Bank::class],
    version = 2,
    exportSchema = false
)

AND THE WHOLE DATABASE CLASS:

    package com.example.holyquran.data.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.example.holyquran.data.model.Bank
import com.example.holyquran.data.model.Loan
import com.example.holyquran.data.model.Transaction
import com.example.holyquran.data.model.UserInfo

@Database(
    entities = [UserInfo::class, Transaction::class, Loan::class,Bank::class],
    version = 2,
    exportSchema = false
)
abstract class UserDatabase : RoomDatabase() {
    abstract val mUserDAO: UserDAO
    abstract val mTransactionsDAO: TransactionsDAO
    abstract val mLoanDAO: LoanDAO
    abstract val mBankDAO: BankDAO

    companion object {
        @Volatile
        private var INSTANCE: UserDatabase? = null
        fun getInstance(context: Context): UserDatabase {
            synchronized(this) {
                var instance = INSTANCE
                if (instance == null) {
                    instance = Room.databaseBuilder(
                        context.applicationContext,
                        UserDatabase::class.java,
                        "user_database"
                    )
                        .allowMainThreadQueries()
                        .fallbackToDestructiveMigration()
                        .build()
                    INSTANCE = instance
                }
                return instance
            }
        }
    }
}

I think It works for you

Here is my project It can help you a lot with room Database My project which has 4 tables

CodePudding user response:

You include the Entity classes in the list/array of entity classes via the entities property of the @Database annotation.

So if the other 6 Entities are T1, T2 ... T6 then

@Database(entities = [StaffTable::class] , version = 1, exportSchema = false)

would become :-

@Database(entities = [StaffTable::class,T1::class,T2::class,T3::class,T4::class,T5::class,T6::class] , version = 1, exportSchema = false)
  • Related