繁体   English   中英

如何从 android 房间数据库中获取特定列?

[英]How to get specific column from android room db?

我试图通过 DAO 中的列名获取列,但没有成功。

@Query("SELECT :columnName FROM info_table")
suspend fun getItem(columnName: String): List<Any>

我有这么多专栏,所以这不是正确的方法。

@Query("SELECT TIME FROM info_table")
suspend fun getTime(): List<Long>

那么我该如何处理呢?

除非您对列名进行硬编码,否则您不能在@Query注释中使用变量作为列名。

但是,您可以使用 RawQuery 例如:-

@RawQuery
fun rawQuery(theQuery: SimpleSQLiteQuery): List<String>
fun getAColumnFromATable(columnName: String, tableName: String): List<String> {
     return rawQuery(SimpleSQLiteQuery("SELECT $columnName FROM $tableName"))
}
  • in which case you use the getAColumnFromATable function, which in this case would return a List which would be capable of getting any values store in the database with the exception of ByteArrays (BLOBS in SQLite terms, in which case you could utilise the SQLite built-在十六进制函数中)。

  • 这超出了您的要求,因为它具有额外的灵活性,可以为任何桌子工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM