如何为 android 房间数据库编写 SQLite 查询
How to write a SQLite query for android room database
我的用户 class 长什么样
@Parcelize
@Entity(tableName = "user_table")
data class User(
@PrimaryKey(autoGenerate = true)
val id: Int,
var name: String,
var highScore: String?,
var activeUser: Boolean,
): Parcelable
我的 UserDao 是什么样子
@Dao
interface UserDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addUser(user: User)
@Update
suspend fun updateUser(user: User)
@Delete
suspend fun deleteUser(user: User)
@Query("DELETE FROM user_table")
suspend fun deleteAllUsers()
@Query("SELECT * FROM user_table ORDER BY id ASC")
fun readAllData(): LiveData<List<User>>
@Query("UPDATE user_table SET activeUser = 0")
fun updateAllActiveUsers()
@Update
fun updateActiveUser(user: User)
}
您好,我想知道如何编写一个查询,使 select 所有活跃用户都为真。所以只有 activeUser = 1
SELECT * FROM user_table WHERE activeUser = true
或者如果执行挂起函数并希望传递布尔值
SELECT * FROM user_table WHERE activeUser = :value
suspend fun getUsers(value: Boolean): T
我的用户 class 长什么样
@Parcelize
@Entity(tableName = "user_table")
data class User(
@PrimaryKey(autoGenerate = true)
val id: Int,
var name: String,
var highScore: String?,
var activeUser: Boolean,
): Parcelable
我的 UserDao 是什么样子
@Dao
interface UserDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun addUser(user: User)
@Update
suspend fun updateUser(user: User)
@Delete
suspend fun deleteUser(user: User)
@Query("DELETE FROM user_table")
suspend fun deleteAllUsers()
@Query("SELECT * FROM user_table ORDER BY id ASC")
fun readAllData(): LiveData<List<User>>
@Query("UPDATE user_table SET activeUser = 0")
fun updateAllActiveUsers()
@Update
fun updateActiveUser(user: User)
}
您好,我想知道如何编写一个查询,使 select 所有活跃用户都为真。所以只有 activeUser = 1
SELECT * FROM user_table WHERE activeUser = true
或者如果执行挂起函数并希望传递布尔值
SELECT * FROM user_table WHERE activeUser = :value
suspend fun getUsers(value: Boolean): T