gpt4 book ai didi

android - 如何插入 "one to one"房间数据库

转载 作者:行者123 更新时间:2023-12-04 07:19:23 26 4
gpt4 key购买 nike

我正在尝试在 android 中插入一个具有一对一关系的对象。
更准确地说,我正在尝试遵循以下示例
googles official documentation
我正在使用两个类 User 和 Library。
我的问题是如何在数据库中插入元素。
我的代码是这样写的

  @Entity
data class Library(
@PrimaryKey(autoGenerate = true) val libraryId: Long = 0,
val userOwnerId: Long
)


@Entity
data class User(
@PrimaryKey(autoGenerate = true) val userId: Long =0,
val name: String,
val age: Int
)

data class UserAndLibrary(
@Embedded val user: User,
@Relation(
parentColumn = "userId",
entityColumn = "userOwnerId"
)
val library: Library
)

插入的逻辑看起来像这样
private val repository = UserRepository(application)

fun insertUser() {
val user1 = User(name = "User1", age = 31)
val user2 = User(name = "User2", age = 32)
val library1 = Library(userOwnerId = user1.userId)
val library2 = Library(userOwnerId = user2.userId)

viewModelScope.launch {
repository.insertUser(user1)

repository.insertUser(user2)

repository.insertLibrary(library1)

repository.insertLibrary(library2)

}

存储库和 dao 类的代码如下所示
//repository
suspend fun insertUser(user: User) = appDataBase.userDao().insertUser(user)

suspend fun insertLibrary(library: Library)=appDataBase.userDao().insertLibrary(library)

//dao
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertUser(user: User)

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertLibrary(library: Library)
问题是库表中的外键始终为0。

最佳答案

我相信您的问题是由于您插入图书馆数据的方式造成的。更具体地说,当您插入库时,您是否设置了库的值 userOwnerId ? (我不相信或正在将其设置为 0)。
考虑以下使用您的类(User、Library 和 userAndLibrary)的问题,其中包含以下 @Dao 和一个非常标准的 @Database :-

@Dao
abstract class AllDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
abstract fun insert(library: Library): Long
@Insert(onConflict = OnConflictStrategy.REPLACE)
abstract fun insert(user: User): Long
@Query("SELECT * FROM user")
abstract fun getAllUsers(): List<User>
@Query("SELECT * FROM library")
abstract fun getAllLibraries(): List<Library>
@Query("SELECT * FROM user")
abstract fun getUsersAndThierLibrary(): List<UserAndLibrary>
}
  • 注意插入返回 Long(即插入行的 id)

  • 结合上述 Activity 中的代码(为了方便和简洁,在主线程上运行):-
        db = TheDatabase.getInstance(this)
    dao = db.getAllDao()

    /* Explanatory Insert */
    var firstUser = User(name = "Susan",age = 20)
    var firstUserId = dao.insert(firstUser)
    dao.insert(Library(userOwnerId = firstUserId))

    /* More Concise Inserts */
    dao.insert(Library(userOwnerId = dao.insert(User(name ="Fred",age = 21))))
    dao.insert(Library(userOwnerId = dao.insert(User(name ="Mary",age= 19))))

    for (u: User in dao.getAllUsers()) {
    Log.d(TAG," User is ${u.name} ID is ${u.userId} age is ${u.age}")
    }
    for (l: Library in dao.getAllLibraries()) {
    Log.d(TAG,"Library is ${l.libraryId} owned by ${l.userOwnerId}")
    }
    for(ual: UserAndLibrary in dao.getUsersAndThierLibrary()) {
    Log.d(TAG,"User = ${ual.user.name} Library ID is ${ual.library.libraryId}")
    }
  • 再次注意在插入用户时使用返回的值( userId )。

  • 可以看出上面插入了 3 个用户和库。然后它提取所有用户、所有库和 UserAndLibrary。结果是:-
    2021-07-31 12:29:41.888 D/MYINFO:  User is Susan ID is 1 age is 20
    2021-07-31 12:29:41.889 D/MYINFO: User is Fred ID is 2 age is 21
    2021-07-31 12:29:41.889 D/MYINFO: User is Mary ID is 3 age is 19
    2021-07-31 12:29:41.890 D/MYINFO: Library is 1 owned by 1
    2021-07-31 12:29:41.890 D/MYINFO: Library is 2 owned by 2
    2021-07-31 12:29:41.890 D/MYINFO: Library is 3 owned by 3
    2021-07-31 12:29:41.894 D/MYINFO: User = Susan Library ID is 1
    2021-07-31 12:29:41.895 D/MYINFO: User = Fred Library ID is 2
    2021-07-31 12:29:41.895 D/MYINFO: User = Mary Library ID is 3
  • 即一切都如预期。
  • 关于android - 如何插入 "one to one"房间数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68598208/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com