英文:
Inserting list<String> in Room Db giving error
问题
以下是翻译好的部分:
我从API获取了一个字符串列表,当我尝试将其插入到Room数据库时,出现了错误 -
参数的类型必须是带有@Entity注释的类或其集合/数组。
这是我的方法
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(list: List<String>). --> 这个方法出错
这是与之对应的表格
@Entity(tableName = "insuree")
public class Insuree {
@ColumnInfo(name = "insurerName")
@NonNull
public String insurerName;
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "_id")
public int id;
}
如何解决这个错误,或者是否有其他方法使这个功能工作?
英文:
I am getting a list of strings from API, same when I tried to insert in Room Db, I am getting an error -
Type of the parameter must be a class annotated with @Entity or a collection/array of it.
Here is my method
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(list: List<String>). --> this method giving error
And this is table for it
@Entity(tableName = "insuree")
public class Insuree {
@ColumnInfo(name = "insurerName")
@NonNull
public String insurerName;
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "_id")
public int id;
}
How can I resolve this error, or is there any other way to get this thing work.
答案1
得分: 1
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(list: List<Insuree>)
在这种情况下,Room 无法确定您要将数据插入到哪个表中。
这是因为使用 @Insert 注释时,Room 根据要插入的对象的类来确定要插入到哪个表中,仅当该类使用 @Entity(这样它才是Room注册/理解的类型)定义时才会如此。然后,可以从类/Entity 中确定在构建底层代码时要使用的实际表名称。
因此,似乎您应该有以下代码:
fun insertAll(list: List<Insuree>)
或者更合适的是:
fun insertAll(insurees: List<Insuree>)
然后,您需要从字符串数组创建一个 Insuree 对象的列表,例如:
val insureeStringList = listOf("Insurer1", "Insurer2", "Insurer3") // Insurer 名称的列表
val insurees = ArrayList<Insuree>()
for (insuree in insureeStringList) {
val i = Insuree()
i.insurerName = insuree
insurees.add(i)
}
// insurees 是 Insuree 对象的列表
appDatabase.insureeDao().insertAll(insurees)
英文:
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(list: List<String>). --> this method giving error
In this situation Room cannot determine which table that you want to insert the data into.
That is when using @Insert annotation Room detemines the table to be inserted into according to the class of the object being inserted and then only if the class is defined using @Entity (so that it is a type that is registered with/understood by Room). The actual table name to use when building the underlying code can then be determined from the class/Entity.
As such it appears that you should have :-
fun insertAll(list: List<Insuree>)
or perhaps more appropriately
fun insertAll(insurees: List<Insuree>)
You would then need to create a list of Insuree objects from your String array e.g.
String[] insureeStringList = new String[]{"Insurer1","Insurer2","Insurer3"}; //<<<<< The list of Insurer names
ArrayList<Insuree> insurees = new ArrayList<>();
Insuree i = new Insuree();
for (String insuree: insureeStringList) {
i.insurerName = insuree;
insurees.add(i);
}
// insurees is a List of Insuree objects
appDatabase.insureeDao().insertAll(insurees);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论