Kotlin 房间数据库布尔值
我正在尝试创建一个应用程序,用户可以在其中列出他们创建的要实现的步骤目标列表,然后选择其中一个进行活动并遵循。当我只使用目标 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将一个新属性(例如
isActive: Boolean
)添加到您的Goal
类中,然后在 Room 中使用@Update
注释(您已经在Dao 的updateGoal(goal: Goal)
方法)或 SQLite 中的UPDATE
命令本身来更新要更改其isActive
状态的行。要使用 SQLite,请执行如下操作:对于布尔属性,在 SQLite 中使用
1
表示true
,使用0
表示false
命令。在 Repository 中,这个方法就足够了:
而在 ViewModel 中:
顺便说一句,您不需要为 Room 方法确定 IO 调度程序,Room 使用自己的调度程序来运行查询。
Just add a new property like
isActive: Boolean
to yourGoal
class and then use@Update
annotation in Room (that you've already implemented inupdateGoal(goal: Goal)
method of your Dao) orUPDATE
command itself in SQLite to update the row you want to change itsisActive
state. For using SQLite do something like below:For boolean properties, use
1
fortrue
and0
forfalse
in SQLite commands.In Repository, this method is enough:
And in the ViewModel:
Btw, you don't need to determine
IO
dispatcher for Room methods, Room uses its own dispatcher to run queries.