Kotlin 房间数据库布尔值

发布于 2025-01-12 21:09:56 字数 1653 浏览 2 评论 0原文

我正在尝试创建一个应用程序,用户可以在其中列出他们创建的要实现的步骤目标列表,然后选择其中一个进行活动并遵循。当我只使用目标 ID、名称和步骤时,数据库可以工作,但现在我意识到我需要插入另一列来定义目标何时处于活动状态,所以我尝试插入该列,但我不知道应该如何处理布尔值,尤其是在存储库和视图模型中。我将不胜感激任何帮助。预先感谢

这是我的代码

interface Dao {

@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(goal: Goal)

@Update
suspend fun updateGoal(goal: Goal)

@Query("SELECT * FROM user_goal_table order by goalId")
fun getAll(): LiveData<List<Goal>>


@Query("SELECT * FROM user_goal_table WHERE goalId = :key")
suspend fun getGoal(key: Int): Goal

@Delete
suspend fun delete(goal: Goal)

@Query("SELECT * FROM user_goal_table WHERE goal_is_active = 1 order by goalId")
suspend fun makeGoalActive(key: Int): Goal




class Repository (private val dao : Dao){

val allGoals: LiveData<List<Goal>> = Dao.getAll()

suspend fun insert(goal: Goal){
    dao.insert(goal)
}

suspend fun update(goal: Goal){
    dao.update(goal)
}

suspend fun delete(goal: Goal){
    dao.delete(goal)
}

suspend fun active(goal: Goal, int: Int){
    dao.makeGoalActive(int)
}



class ViewModel (application: Application) : AndroidViewModel(application) {

val allGoals: LiveData<List<Goal>>
private val repository: Repository

init{
    val dao = GoalDatabase.getInstance(application).getGoalDao()
    repo = Repository(dao)
    allGoals = repository.allGoals

}


fun insert(goal: Goal) = viewModelScope.launch(Dispatchers.IO){
    repository.insert(goal)
}

fun update(goal: Goal) = viewModelScope.launch(Dispatchers.IO){
    repository.update(goal)
}

fun delete(goal: Goal) = viewModelScope.launch(Dispatchers.IO){
    repository.delete(goal)
}

Im trying to create an app where a user can has a list of goals of steps to reach they create and then choose one of them to be active and to follow. The database works when I was just using the goal id, name, and steps but now I realised I need to insert another column defining when a goal is active so Im trying to insert that, however I don't know how I should handle the boolean especially in the repository and viewModel. I'd appreciate any help. Thanks in advance

here's my code

interface Dao {

@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insert(goal: Goal)

@Update
suspend fun updateGoal(goal: Goal)

@Query("SELECT * FROM user_goal_table order by goalId")
fun getAll(): LiveData<List<Goal>>


@Query("SELECT * FROM user_goal_table WHERE goalId = :key")
suspend fun getGoal(key: Int): Goal

@Delete
suspend fun delete(goal: Goal)

@Query("SELECT * FROM user_goal_table WHERE goal_is_active = 1 order by goalId")
suspend fun makeGoalActive(key: Int): Goal




class Repository (private val dao : Dao){

val allGoals: LiveData<List<Goal>> = Dao.getAll()

suspend fun insert(goal: Goal){
    dao.insert(goal)
}

suspend fun update(goal: Goal){
    dao.update(goal)
}

suspend fun delete(goal: Goal){
    dao.delete(goal)
}

suspend fun active(goal: Goal, int: Int){
    dao.makeGoalActive(int)
}



class ViewModel (application: Application) : AndroidViewModel(application) {

val allGoals: LiveData<List<Goal>>
private val repository: Repository

init{
    val dao = GoalDatabase.getInstance(application).getGoalDao()
    repo = Repository(dao)
    allGoals = repository.allGoals

}


fun insert(goal: Goal) = viewModelScope.launch(Dispatchers.IO){
    repository.insert(goal)
}

fun update(goal: Goal) = viewModelScope.launch(Dispatchers.IO){
    repository.update(goal)
}

fun delete(goal: Goal) = viewModelScope.launch(Dispatchers.IO){
    repository.delete(goal)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声无音无过去 2025-01-19 21:09:56

只需将一个新属性(例如 isActive: Boolean)添加到您的 Goal 类中,然后在 Room 中使用 @Update 注释(您已经在Dao 的 updateGoal(goal: Goal) 方法)或 SQLite 中的 UPDATE 命令本身来更新要更改其 isActive 状态的行。要使用 SQLite,请执行如下操作:

@Query("UPDATE user_goal_table SET isActive = 1 WHERE goalId = :goalId")
suspend fun makeGoalActive(goalId: Int)

对于布尔属性,在 SQLite 中使用 1 表示 true,使用 0 表示 false命令。

在 Repository 中,这个方法就足够了:

suspend fun active(goalId: Int) {
    dao.makeGoalActive(int)
}

而在 ViewModel 中:

fun insert(goal: Goal) = viewModelScope.launch {
    repository.insert(goal)
}

顺便说一句,您不需要为 Room 方法确定 IO 调度程序,Room 使用自己的调度程序来运行查询。

Just add a new property like isActive: Boolean to your Goal class and then use @Update annotation in Room (that you've already implemented in updateGoal(goal: Goal) method of your Dao) or UPDATE command itself in SQLite to update the row you want to change its isActive state. For using SQLite do something like below:

@Query("UPDATE user_goal_table SET isActive = 1 WHERE goalId = :goalId")
suspend fun makeGoalActive(goalId: Int)

For boolean properties, use 1 for true and 0 for false in SQLite commands.

In Repository, this method is enough:

suspend fun active(goalId: Int) {
    dao.makeGoalActive(int)
}

And in the ViewModel:

fun insert(goal: Goal) = viewModelScope.launch {
    repository.insert(goal)
}

Btw, you don't need to determine IO dispatcher for Room methods, Room uses its own dispatcher to run queries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文