无法将Android Room插入表中

时间:2018-10-31 21:47:56

标签: android database sqlite kotlin

我一直在环顾四周,找不到任何解释为什么会这样。 我试过在sqliteman之类的应用程序中打开数据库文件,然后自己运行“ INSERT INTO”查询,那样工作就很好。但是,无论Room是否似乎无法正常工作,它都不会崩溃或发生任何事情,它只是不会插入任何内容。

所以基本上我有2个表通过联接表以m-n关系连接:

1)歌曲表

@Entity(tableName = RoomDbConstants.TABLE_NAME_SONGS,
    foreignKeys = [ForeignKey(entity = Album::class,
            parentColumns = [RoomDbConstants.COLUMN_ALBUM_ARTIST_NAME, RoomDbConstants.COLUMN_ALBUM_NAME],
            childColumns = [RoomDbConstants.COLUMN_SONG_ARTIST_NAME, RoomDbConstants.COLUMN_SONG_ALBUM_NAME],
            onDelete = ForeignKey.CASCADE),
        ForeignKey(entity = Genre::class,
                parentColumns = [RoomDbConstants.COLUMN_GENRE_NAME],
                childColumns = [RoomDbConstants.COLUMN_SONG_GENRE_NAME],
                onDelete = ForeignKey.CASCADE)])
data class Song(
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = RoomDbConstants.COLUMN_SONG_ID)
    val id: Long?,
    @ColumnInfo(name = RoomDbConstants.COLUMN_SONG_DATA)
    val songData: String,
    @ColumnInfo(name = RoomDbConstants.COLUMN_SONG_NAME)
    val name: String,
    @ColumnInfo(name = RoomDbConstants.COLUMN_SONG_ALBUM_NAME)
    val albumName: String,
    @ColumnInfo(name = RoomDbConstants.COLUMN_SONG_ARTIST_NAME)
    val artistName: String,
    @ColumnInfo(name = RoomDbConstants.COLUMN_SONG_GENRE_NAME)
    val genreName: String,
    @ColumnInfo(name = RoomDbConstants.COLUMN_SONG_TRACK_NUMBER)
    val trackNumber: Int = RoomDbConstants.TRACK_NUMBER_DEFAULT,
    @ColumnInfo(name = RoomDbConstants.COLUMN_SONG_YEAR_PUBLISHED)
    val yearPublished: Int = RoomDbConstants.YEAR_PUBLISHED_NONE,
    @ColumnInfo(name = RoomDbConstants.COLUMN_SONG_START_TIME_IN_MILLIS)
    val startTimeInMillis: Int,
    @ColumnInfo(name = RoomDbConstants.COLUMN_SONG_END_TIME_IN_MILLIS)
    val endTimeInMillis: Int,
    @ColumnInfo(name = RoomDbConstants.COLUMN_SONG_DURATION_IN_MILLIS)
    val durationInMillis: Int,
    @ColumnInfo(name = RoomDbConstants.COLUMN_SONG_COVER_DATA)
    val coverData: String?)

2)播放列表表

@Entity(tableName = RoomDbConstants.TABLE_NAME_PLAYLISTS,
    indices = [Index(value = [RoomDbConstants.COLUMN_PLAYLIST_NAME], unique = true)])
data class Playlist(
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = RoomDbConstants.COLUMN_PLAYLIST_ID)
    val id: Long?,
    @ColumnInfo(name = RoomDbConstants.COLUMN_PLAYLIST_NAME)
    val name: String,
    @ColumnInfo(name = RoomDbConstants.COLUMN_PLAYLIST_COVER_DATA)
    val coverData: String?)

3)SongsPlaylistsLink(在Songs和Playlist之间连接的表)

@Entity(tableName = RoomDbConstants.TABLE_NAME_SONGS_PLAYLISTS_LINKS,
    primaryKeys = [RoomDbConstants.COLUMN_SONGS_PLAYLISTS_LINK_SONG_ID,
        RoomDbConstants.COLUMN_SONGS_PLAYLISTS_LINK_PLAYLIST_ID],
    foreignKeys = [ForeignKey(entity = Song::class,
            parentColumns = [RoomDbConstants.COLUMN_SONG_ID],
            childColumns = [RoomDbConstants.COLUMN_SONGS_PLAYLISTS_LINK_SONG_ID],
            onDelete = ForeignKey.CASCADE),
        ForeignKey(entity = Playlist::class,
                parentColumns = [RoomDbConstants.COLUMN_PLAYLIST_ID],
                childColumns = [RoomDbConstants.COLUMN_SONGS_PLAYLISTS_LINK_PLAYLIST_ID],
                onDelete = ForeignKey.CASCADE)],
    indices = [Index(value = [RoomDbConstants.COLUMN_SONGS_PLAYLISTS_LINK_SONG_ID,
    RoomDbConstants.COLUMN_SONGS_PLAYLISTS_LINK_PLAYLIST_ID], unique = true)])
data class SongsPlaylistsLink(
    @ColumnInfo(name = RoomDbConstants.COLUMN_SONGS_PLAYLISTS_LINK_SONG_ID)
    val songId: Long,
    @ColumnInfo(name = RoomDbConstants.COLUMN_SONGS_PLAYLISTS_LINK_PLAYLIST_ID)
    val playlistId: Long)

插入功能:

@Insert
fun insert(songsPlaylistsLink: SongsPlaylistsLink)

所以这是我尝试做的事情: 我的数据库中已经有歌曲和播放列表。 假设我有一首ID为1的歌曲和一首ID为1的播放列表。 当我插入(SongsPlaylistsLink(1,1))时,什么也没发生。 该表仍然为空。

任何对这种情况发生的帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

看起来分别在insert语句之前和之后运行db.beginTransaction()和db.endTransaction()不能很好地工作。删除它会为我修复。