Room 可以使用复合主键进行查询吗?
@Entity(
tableName = "market_cap",
primaryKeys = ["assetId", "timestamp"]
)
data class MarketCapDataEntity(
val assetId: String,
val timestamp: Long,
val price: Double
)
假设我想按主键执行查询,如下所示?
@Query("SELECT * FROM market_cap WHERE primaryKeys = :compositeKey")
suspend fun getMarketCapByAssetId(compositeKey: String): List<MarketCapDataEntity>
您需要在 SQL 查询中指定主键的每个组成部分,而不是尝试使用单个组合键
@Entity(
tableName = "market_cap",
primaryKeys = ["assetId", "timestamp"]
)
data class MarketCapDataEntity(
val assetId: String,
val timestamp: Long,
val price: Double
)
@Dao
interface MarketCapDao {
@Query("SELECT * FROM market_cap WHERE assetId = :assetId AND timestamp = :timestamp")
suspend fun getMarketCapByAssetIdAndTimestamp(assetId: String, timestamp: Long): MarketCapDataEntity?
}