简体   繁体   English

如何在 1 个房间数据库中创建多个表列?

[英]How to create multiple table columns in 1 room database?

I have created 1 room database following best practices in kotlin.我按照 kotlin 中的最佳实践创建了 1 个房间数据库。 I want to create 2 table columns within 1 database.我想在 1 个数据库中创建 2 个表列。 How can I stack tables within the database?如何在数据库中堆叠表? Any advice is appreciated!任何建议表示赞赏!

Something similar to this: https://gyazo.com/6327f163f463a4b19b59c5aece2136e3与此类似的东西: https://gyazo.com/6327f163f463a4b19b59c5aece2136e3

Age database: https://gyazo.com/0074f2e4f002de9a3ad07ec593a826d4年龄数据库: https://gyazo.com/0074f2e4f002de9a3ad07ec593a826d4

Gender database: https://gyazo.com/c8b800b8f2f4fc6b43a9de2ddfdb1e7d性别数据库: https://gyazo.com/c8b800b8f2f4fc6b43a9de2ddfdb1e7d

My age database:我的年龄数据库:

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

    abstract fun AgeDao() : AgeDao

    companion object {

        @Volatile
        private var INSTANCE : AgeDb? = null

        fun getDatabase(context: Context) : AgeDb {

            val tempInstance = INSTANCE
            if (tempInstance != null) {

                return tempInstance
            }

            synchronized(this) {

                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    AgeDb::class.java,
                    "dropdown_age"
                ).build()
                INSTANCE = instance
                return instance
            }

        }
    }
}

i see your code.我看到你的代码。 actually,I can't understand why don't you use only one database?实际上,我不明白你为什么不只使用一个数据库? and create two table,even you can only create one table to save age attribute and gender attribute.并创建两张表,即使您只能创建一张表来保存年龄属性和性别属性。 so,maybe you would think about it, did you design the right databases and tabels所以,也许您会考虑一下,您是否设计了正确的数据库和表格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM