“Room Dao 查询返回“0”,尽管表列中有正值”

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

Room Dao query returning "0" despite positive value in the table column

问题

查询下面的购物清单Dao返回"0"作为list_total列的值,即使数据库中的值是"3048"。代码可能有什么问题?

Dao

@Query("SELECT * FROM shopping_lists WHERE is_include_in_lists_total = 1")
suspend fun getAllIncludedInTotalShoppingLists(): List<ShoppingListEntity>

ShoppingListModel

@Parcelize
data class ShoppingList(
    val id: Long = 0L,
    val name: String = "",
    val numberOfItems: Long = 0L,
    val listTotal: String = "0",
    val isCustomSortEdited: Boolean = false,
    val sortOrder: String = "None",
    val isIncludeInListsTotal: Boolean = false,
) : Parcelable

ShoppingListEntity

@Entity(tableName = "shopping_lists")
data class ShoppingListEntity(
    @PrimaryKey(autoGenerate = true)
    var id: Long = 0L,
    @ColumnInfo(name = "name")
    val name: String,
    @ColumnInfo(name = "number_of_items")
    val numberOfItems: Long = 0L,
    @ColumnInfo(name = "list_total")
    val listTotal: String,
    @ColumnInfo(name = "is_custom_sort_edited")
    val isCustomSortEdited: Boolean = false,
    @ColumnInfo(name = "sort_order")
    val sortOrder: String,
    @ColumnInfo(name = "is_include_in_lists_total")
    val isIncludeInListsTotal: Boolean = false,
)

Repository

override suspend fun getAllIncludedInTotalShoppingLists(): List<ShoppingList> {
   return withContext(Dispatchers.IO) {
        val returnedList = shoppingListDao.getAllIncludedInTotalShoppingLists().map {
            shoppingListMapper.mapFromEntity(it)
        }

        return@withContext returnedList
    }
}

Mapper

class ShoppingListMapper : EntityMapper<ShoppingListEntity, ShoppingList> {
    override fun mapFromEntity(entity: ShoppingListEntity, includeId: Boolean): ShoppingList {
        return if(includeId) {
            ShoppingList(
                id = entity.id,
                name = entity.name,
                numberOfItems = entity.numberOfItems,
                listTotal = entity.listTotal,
                sortOrder = entity.sortOrder,
                isCustomSortEdited = entity.isCustomSortEdited,
                isIncludeInListsTotal = entity.isIncludeInListsTotal
            )
        } else ShoppingList(
            name = entity.name,
            numberOfItems = entity.numberOfItems,
            listTotal = entity.listTotal,
            sortOrder = entity.sortOrder,
            isCustomSortEdited = entity.isCustomSortEdited,
            isIncludeInListsTotal = entity.isIncludeInListsTotal
        )
    }
}
英文:

The shopping list Dao query below is returning &quot;0&quot; for the list_total column even though the value in the database is &quot;3048&quot;. What could be wrong with the code?

Dao

@Query(&quot;SELECT * FROM shopping_lists WHERE is_include_in_lists_total = 1&quot;)
suspend fun getAllIncludedInTotalShoppingLists(): List&lt;ShoppingListEntity&gt;

ShoppingListModel

@Parcelize
data class ShoppingList(
    val id: Long = 0L,
    val name: String = &quot;&quot;,
    val numberOfItems: Long = 0L,
    val listTotal: String = &quot;0&quot;,
    val isCustomSortEdited: Boolean = false,
    val sortOrder: String = &quot;None&quot;,
    val isIncludeInListsTotal: Boolean = false,
) : Parcelable

ShoppingListEntity

@Entity(tableName = &quot;shopping_lists&quot;)
data class ShoppingListEntity(
    @PrimaryKey(autoGenerate = true)
    var id: Long = 0L,
    @ColumnInfo(name = &quot;name&quot;)
    val name: String,
    @ColumnInfo(name = &quot;number_of_items&quot;)
    val numberOfItems: Long = 0L,
    @ColumnInfo(name = &quot;list_total&quot;)
    val listTotal: String,
    @ColumnInfo(name = &quot;is_custom_sort_edited&quot;)
    val isCustomSortEdited: Boolean = false,
    @ColumnInfo(name = &quot;sort_order&quot;)
    val sortOrder: String,
    @ColumnInfo(name = &quot;is_include_in_lists_total&quot;)
    val isIncludeInListsTotal: Boolean = false,
)

Repository

override suspend fun getAllIncludedInTotalShoppingLists(): List&lt;ShoppingList&gt; {
   return withContext(Dispatchers.IO) {
        val returnedList = shoppingListDao.getAllIncludedInTotalShoppingLists().map {
            shoppingListMapper.mapFromEntity(it)
        }

        return@withContext returnedList
    }
}

Mapper

class ShoppingListMapper : EntityMapper&lt;ShoppingListEntity, ShoppingList&gt; {
    override fun mapFromEntity(entity: ShoppingListEntity, includeId: Boolean): ShoppingList {
        return if(includeId) {
            ShoppingList(
                id = entity.id,
                name = entity.name,
                numberOfItems = entity.numberOfItems,
                listTotal = entity.listTotal,
                sortOrder = entity.sortOrder,
                isCustomSortEdited = entity.isCustomSortEdited,
                isIncludeInListsTotal = entity.isIncludeInListsTotal
            )
        } else ShoppingList(
            name = entity.name,
            numberOfItems = entity.numberOfItems,
            listTotal = entity.listTotal,
            sortOrder = entity.sortOrder,
            isCustomSortEdited = entity.isCustomSortEdited,
            isIncludeInListsTotal = entity.isIncludeInListsTotal
        )
    }
}

答案1

得分: 1

你的查询目前只会选择值为1的项目,3048不等于1。

你可以使用以下查询:

"SELECT * FROM shopping_lists WHERE is_include_in_lists_total"
  • 因为WHERE需要从后续表达式中获得逻辑/布尔值,所以对于数值,任何非零值(零为假)都将为真(例如3048)。
  • 根据以下链接:

查看
> 2.3. WHERE子句过滤。
>
>如果指定了WHERE子句,则对输入数据中的每一行都将WHERE表达式评估为布尔表达式。仅在继续之前,WHERE子句表达式评估为true的行才包括在数据集中。如果WHERE子句评估为false或NULL,则排除结果中的行。
>
> - https://www.sqlite.org/lang_select.html#whereclause
>
> 关于布尔表达式的信息
>
>为了将SQL表达式的结果转换为布尔值,SQLite首先将结果转换为NUMERIC值,方式与CAST表达式相同。数值零值(整数值0或实数值0.0)被视为false。NULL值仍然为NULL。所有其他值被视为true。
>
>例如,NULL、0.0、0、'english'和'0'都被视为false。值1、1.0、0.1、-0.1和'1english'都被视为true。
> - https://www.sqlite.org/lang_expr.html#booleanexpr

  • 你也可以使用"SELECT * FROM shopping_lists WHERE is_include_in_lists_total > 0"
英文:

Your query as it stands would only select values that are 1, 3048 is not equal to 1.

You could use

&quot;SELECT * FROM shopping_lists WHERE is_include_in_lists_total&quot;
  • as WHERE expects a logical/boolean from the ensuing expression so for numerical values any non-zero value (zero being false) will be true (such as 3048).
  • as per

See
> 2.3. WHERE clause filtering.
>
>If a WHERE clause is specified, the WHERE expression is evaluated for each row in the input data as a boolean expression. Only rows for which the WHERE clause expression evaluates to true are included from the dataset before continuing. Rows are excluded from the result if the WHERE clause evaluates to either false or NULL.
>
> - https://www.sqlite.org/lang_select.html#whereclause
>
> and in regards to boolean expressions
>
>To convert the results of an SQL expression to a boolean value, SQLite first casts the result to a NUMERIC value in the same way as a CAST expression. A numeric zero value (integer value 0 or real value 0.0) is considered to be false. A NULL value is still NULL. All other values are considered true.
>
>For example, the values NULL, 0.0, 0, 'english' and '0' are all considered to be false. Values 1, 1.0, 0.1, -0.1 and '1english' are considered to be true.
> - and https://www.sqlite.org/lang_expr.html#booleanexpr

  • You could also use &quot;SELECT * FROM shopping_lists WHERE is_include_in_lists_total &gt; 0&quot;.

答案2

得分: 0

你可以尝试这样做。

@Query("SELECT * FROM SHOPPINGLISTENTITY WHERE is_include_in_lists_total LIKE 1")

我认为问题在于你写错了表名 shopping_lists,请尝试使用 LIKE 而不是 = ,希望这样可以解决你的问题。

英文:

You can try it like this.

@Query(&quot;SELECT * FROM SHOPPINGLISTENTITY WHERE is_include_in_lists_total LIKE 1&quot;)

I think the problem is you write the wrong table name shopping_lists try and use LIKE instead of = I hope your problem is solved using this

huangapple
  • 本文由 发表于 2023年1月6日 11:22:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026626.html
匿名

发表评论

匿名网友

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

确定