在 Room 数据库中插入 list 时出现错误。

huangapple go评论64阅读模式
英文:

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&lt;String&gt;).        --&gt; this method giving error

And this is table for it

@Entity(tableName = &quot;insuree&quot;)
public class Insuree {
    @ColumnInfo(name = &quot;insurerName&quot;)
    @NonNull
    public String insurerName;

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = &quot;_id&quot;)
    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&lt;String&gt;).        --&gt; 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&lt;Insuree&gt;) 

or perhaps more appropriately

fun insertAll(insurees: List&lt;Insuree&gt;)

You would then need to create a list of Insuree objects from your String array e.g.

    String[] insureeStringList = new String[]{&quot;Insurer1&quot;,&quot;Insurer2&quot;,&quot;Insurer3&quot;}; //&lt;&lt;&lt;&lt;&lt; The list of Insurer names
    ArrayList&lt;Insuree&gt; insurees = new ArrayList&lt;&gt;();
    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);

huangapple
  • 本文由 发表于 2020年1月3日 18:53:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577281.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定