简体   繁体   English

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

[英]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.

相关问题 如何使用 Android Room 获取特定列 - How to get specific column using Android Room 如何在 android 中的房间 db 中获取映射到 pojo 的列信息 - how to get column info mapped to pojo in room db in android android-如何使用Room从数据库获取简单列表? - android - How to get simple list from db using Room? 在 Room db Dao 类中 - 如何通过比较列的不同值从表中获取值? - In Room db Dao class- how to get values from table by comparing distinct values of a column? Android,如何检查 Room DB 中是否已存在列? - Android, how to check if column already exists in Room DB? 针对房间 DB android 中的特定列插入或更新一列的值 - Insert or Update value of one column against specific column in room DB android 在 Android 的 Spinner 中选择项目时如何从 ROOM 数据库中获取特定数据 - How to get specific data from ROOM database when an item is selected in Spinner in Android 给定房间查询中的特定参数,如何从房间数据库中选择特定列? - How to select a specific column from room database given a specific parameter in room query? 如何根据日期/时间戳从 Room DB 中获取排序数据? - How to get sorted data from Room DB on the base of Date/TimeStamp? 如何从Android中自己的SQL DB获取特定项目 - How to get specific item from own SQL DB in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM