英文:
Storing data to Room database
问题
我有一个问题,
假设我们有一个应用程序,在其中我们在运行时添加许多类别(例如:购物,学习,公共等)。由于我们是在运行应用程序时添加它们的,并将它们存储在一个名为 RoomDatabase 的数据库中,该数据库具有一个实体类(Entity class)和一个数据访问对象(Dao)。
-> 现在我的问题出现在这里,在这些类别(例如:购物,学习,公共等)中,我们将存储许多不同的物品,那么如何使用 RoomDatabase 来存储所有这些类别的物品。
因此,当我们打开购物类别时,它只会显示购物列表,其他类别也是如此。
是否有可能保存所有这些类别的数据呢? 由于我们正在添加许多类别和它们的子数据,那么如何能够创建那么多的 DAO 和 ENTITY CLASS 来保存那么多的子数据。
-> 如果我们使用 SHARED PREFENCES,我们可以将所有类别的子数据存储在唯一的键中(唯一键可以是类别名称)。因此,如果我们添加更多的类别,它将把数据存储到类别的唯一名称中。
但是在 RoomDatabase 中如何实现呢?如果可以的话,应该如何实现呢?
我希望我解释清楚了我的问题。
Dao 接口
@Dao
interface Dao {
@Insert
fun insert(studentName: StudentsEntity): Long
@Insert
fun stdDetailsInsert(stdDetails: StudentDetailsEntity)
@Transaction
@Query("Select * from Student")
fun getAllStudent(): MutableList<WholeDataStudent>
@Query("Select * from Student")
fun getStudentName(): MutableList<StudentsEntity>
}
Entity 类
@Entity(tableName = "Student")
data class StudentsEntity(
val stdName: String
) {
@PrimaryKey(autoGenerate = true)
var stdId: Int = 0
}
@Entity
data class StudentDetailsEntity(
val detailsId: Int,
val stdAddress: String,
val phoneNo: String
) {
@PrimaryKey(autoGenerate = true)
var id: Int = 0
}
data class WholeDataStudent(
@Embedded val std: StudentsEntity,
@Relation(
parentColumn = "stdId",
entityColumn = "detailsId",
entity = StudentDetailsEntity::class
)
val stdDetails: List<StudentDetailsEntity>
)
英文:
I have a question that,
Let's consider we have a app in which we are adding many categories (ex: shopping,study,public, etc..) at runtime , there is no limit of adding categories since we adding it at runtime while we use the app and storing in a RoomDatabase having one Entity class and Dao.
-> now my question arises here , In that categories(ex: shopping,study,public, etc..) there are lot of different items we will store , so how can we use Roomdatabase to store all these categories item .
so that when we will open the shopping category so it will show only shopping list and so on for other categories
Is it possible to save all data of these categories? ,since we are adding many categories and there sub data then how it is possible to make that much of DAO and ENTITY CLASS to save that much of sub data.
-> If we will use SHARED PREFENCES . then we can store all categories sub data into a unique keys(unique key can be a categories name). so if we add more and more categories then it will store data to categories unique name.
but how can we do in roomDatabase. Is it possible? if yes then how....?
i hope, i explained my question well
Dao Interface
@Dao
interface Dao {
@Insert
fun insert(studentName: StudentsEntity): Long
@Insert
fun stdDetailsInsert(stdDetails: StudentDetailsEntity)
@Transaction
@Query("Select * from Student")
fun getAllStudent(): MutableList<WholeDataStudent>
@Query("Select * from Student")
fun getStudentName(): MutableList<StudentsEntity>
}
Entity Class
@Entity(tableName = "Student")
data class StudentsEntity(
val stdName: String
) {
@PrimaryKey(autoGenerate = true)
var stdId: Int = 0
}
@Entity
data class StudentDetailsEntity(
val detailsId: Int,
val stdAddress: String,
val phoneNo: String
) {
@PrimaryKey(autoGenerate = true)
var id: Int = 0
}
data class WholeDataStudent(
@Embedded val std: StudentsEntity,
@Relation(
parentColumn = "stdId",
entityColumn = "detailsId",
entity = StudentDetailsEntity::class
)
val stdDetails: List<StudentDetailsEntity>
)
答案1
得分: 3
关于你不同类别中的项目,这些项目是否可以使用相同的“Item”实体类模板?
我可以为两种情况提供解决方案。
在没有进一步的信息的情况下,让我给你提供一个支持动态添加类别(及相应项目)并且适用于所有项目的通用项目类的想法。
基于 SQL 的 Room 提供了多种定义数据关系的方式。
据我所理解的你的问题:
- 一个类别可以有多个项目
- 但每个项目只属于一个类别
如果这是正确的,那么你在数据中有一个一对多关系。
Akash Bisht 指出的是:你应该为类别有另一个实体。
在那里,你至少需要存储诸如 ID(主键)和该类别的名称(字符串)等信息。
然后你可以动态创建类别并将它们添加到这个表中。这可以是任意多的类别。
然后在创建属于此类别的项目时,你只需在“Item”实体中添加对应类别的 ID 引用,例如作为一个名为 categoryId
的变量。
另一方面,你也可以直接在项目实体中存储类别名称。但这样做的缺点是,当你想要更改例如类别名称时,你必须更新所有项目。
然后,要仅获取特定类别的项目,就像你在要求中所述,你至少有两个选项:
- 编写一个 SQL 查询,仅选择特定类别的项目
- 或者你查询所有项目的列表,然后按类别对它们进行过滤
如果你不同类别的数据是可以存储在共享首选项中的类型(正如你所提到的)。你也可以使用这种方法处理。
英文:
For your items in your different categories, can these items use the same Item
Entity class template?
I can think of solutions for both scenarios.
Without further information, let me give you an idea for supporting adding categories (and corresponding items) dynamically and a common item class for all items.
Room (based on SQL) offers several ways of to define data relationships.
As far as I understand your problem:
- one category can have many items
- but each item belongs only to one category
If that holds true, then you have a one-to-many relationship in your data.
What Akash Bisht was pointing at: you should have another entity for your categories.
There, at minimum you need to store information such as the ID (primary key) and the name (string) of that category.
Then you can create categories dynamically and add them to this table. This can be as many categories as you want.
Then when creating your items inside this category, you just need to add the ID reference of the corresponding category in the Item
entity, e.g. as a variable like categoryId
On the other hand you could also just store the category name directly in the Item entity. But this has the disadvantage that when you want to change, for example, your categories name, then you have to update all your items.
To then just get the items for one specific category, as you stated as requirements, you have at least 2 options:
- write a SQL query that only selects items of a specific category
- or you query a list of all your items and then filter them by a category
If your data for different categories is of types that can be stored in shared preferences (as you have mentioned). You also can handle that with this approach.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论