【发布时间】:2019-10-01 10:56:26
【问题描述】:
我必须在数据库中插入一个帖子,它是多个图像,所以我创建了以下表格
张贴表:
@Entity
data class PostTable(
var post: String,
var images : ArrayList<String>) : Parcelable{
@PrimaryKey(autoGenerate = true)
var id: Int = 0
}
图像表:
@Entity(foreignKeys = arrayOf(ForeignKey(entity = PostTable::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("imd"),
onDelete = ForeignKey.CASCADE)))
data class ImageTable (
@PrimaryKey(autoGenerate = true)
val id: Int,
val imd: Int,
val imageURL: String
)
道帖表:
@Dao
interface PostDao {
@get:Query("SELECT * FROM PostTable ORDER BY id ASC")
val posts: LiveData<MutableList<PostTable>>
@Insert
fun insert(post: PostTable)
}
道影像表:
@Dao
interface ImageDao {
@Insert
fun insert(images: ImageTable)
@Query("SELECT * FROM ImageTable WHERE imd=:postID")
fun allImages(postID: Int): List<ImageTable>
}
我可以在 PostTable 中插入值,如何通过获取 PostTable 的 id 来将值插入到 PostTable 和它的图像到 ImageTable 中?
【问题讨论】:
标签: android sqlite android-sqlite android-room