[英]How to get specific column from android room db?
I tried to get columns through column names in DAO, but it didn't work.我试图通过 DAO 中的列名获取列,但没有成功。
@Query("SELECT :columnName FROM info_table")
suspend fun getItem(columnName: String): List<Any>
I have so many columns so It is not proper approach.我有这么多专栏,所以这不是正确的方法。
@Query("SELECT TIME FROM info_table")
suspend fun getTime(): List<Long>
So How can i deal with it?那么我该如何处理呢?
Unless you hard code the column names you cannot use a variable for the column name in an @Query
annotation.除非您对列名进行硬编码,否则您不能在
@Query
注释中使用变量作为列名。
However, you could utilise a RawQuery eg :-但是,您可以使用 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-in hex function). 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-在十六进制函数中)。
this is more than you asked for as it has the additional flexibility of being able to work for any table.这超出了您的要求,因为它具有额外的灵活性,可以为任何桌子工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.