ROOM持久性库更新和删除不会改变任何东西

我正在编写小型应用程序,并为其使用空间。SQLite 包装器。就我的代码工作而言,有一些功能不起作用。此外,文档似乎非常不完整,或者在某些方面是错误的。例如dao注解@Update和@Delete。我慢慢地对使用这个框架感到困惑,并且我考虑重新使用 SQLite API 而不是它。您对房间有何看法?

现在是我想问的主要问题。我将向您展示我的代码,也许您会明白为什么不能正常工作。

首先,我的实体和道。它应该代表一个音频文件。

@Entity(tableName = "audiofile")

public class AudioFile

{

    @PrimaryKey(autoGenerate = true)

    private long id;


    @ColumnInfo(name = "title")

    private String title;


    @ColumnInfo(name = "artist")

    private String artist;


    @ColumnInfo(name = "path")

    private String path;


    @ColumnInfo(name = "duration")

    private long duration;


@Dao

public interface AudioFileDao

{

    @Query("SELECT * FROM audiofile WHERE title LIKE :title")

    AudioFile getAudioFile(String title);


    @Query("SELECT * FROM audiofile")

    List<AudioFile> getAll();


    @Insert(onConflict = OnConflictStrategy.REPLACE)

    void initAudioFiles(AudioFile... audioFiles);


    @Update

    void updateAudioFile(AudioFile... audioFiles);


//        @Query("DELETE FROM audiofile WHERE id = :audioFileId")

    @Delete

    void deleteAudioFile(AudioFile audioFile);


    @Insert

    void insertAll(AudioFile... audioFiles);


    @Insert

    long insertAudioFile(AudioFile audioFile);

}

那是常规的数据库管理器。


@Database(entities = {AudioFile.class}, version = 1)

public abstract class DbManager extends RoomDatabase

{


    private static DbManager INSTANCE;


    public abstract AudioFile.AudioFileDao audioFileDao();


    public static DbManager getDbManager(Context ctx){

        if (INSTANCE == null){

            INSTANCE = Room.databaseBuilder(ctx.getApplicationContext(), DbManager.class, "playlist").build();

        }

        return INSTANCE;

    }


    public static void destroyInstance(){

        INSTANCE = null;

    }

}


UYOU
浏览 133回答 1
1回答

慕标琳琳

现在是我想问的主要问题。我将向您展示我的代码,也许您会明白为什么不能正常工作。我相信您的问题是,当您插入 AudioFile 时,未设置 id,因此 AudioFile 对象 audioFile 中的 id 为 0。但是,插入时,id 将自动生成,并且不会为 0,而可能是某个时间插入的 AudioFile 对象的数量 + 1。如果不设置id,则使用相同的 AudioFile 对象来更新/删除将尝试使用0的 id 来更新删除。不会有这样的行。假设AudioFile 存在setId方法,但不处理失败的插入,可以使用以下修复:-audioFile.setId(audioFileDao.insertAudioFile(audioFile));可以处理未导致异常的失败插入的修复可能是:-long insertedId = audioFileDao.insertAudioFile(audioFile);if (insertedId > 0) {&nbsp; &nbsp; audioFile.setId(insertedId);} else {&nbsp; &nbsp; //......... handle not inserted}示例/演示以下代码运行时显示上述内容(您的 Entity 和 Dao 已按原样复制):-&nbsp; &nbsp; ...... database built&nbsp; &nbsp; mAudioFileDao = mAppDB.audioFileDao();&nbsp; &nbsp; AudioFile a = new AudioFile();&nbsp; &nbsp; a.setTitle("MySong");&nbsp; &nbsp; a.setDuration(5);&nbsp; &nbsp; a.setArtist("Fred");&nbsp; &nbsp; a.setPath("/thepath");&nbsp; &nbsp; logAudioFile(a,"Before Insert into DB");&nbsp; &nbsp; long currentId = mAudioFileDao.insertAudioFile(a); //<<<<<<<<<< INSERT INTO DB&nbsp; &nbsp; logAudioFile(a,"Immediately after Insert. ID returned from insert is " + currentId);&nbsp; &nbsp; a.setId(currentId); //<<<<<<<<<< SET the the id of the AudioFile object a&nbsp; &nbsp; logAudioFile(a,"After setting the ID to " + currentId);&nbsp; &nbsp; mAudioFileDao.updateAudioFile(a);&nbsp; &nbsp; List<AudioFile> audioFileList = mAudioFileDao.getAll();&nbsp; &nbsp; for (AudioFile af: audioFileList) {&nbsp; &nbsp; &nbsp; &nbsp; logAudioFile(af,"Extracted from DB");&nbsp; &nbsp; }&nbsp; &nbsp; mAudioFileDao.deleteAudioFile(a);&nbsp; &nbsp; Log.d("AUDIOFILEINFO","Attempt to delete Audio File undertaken");&nbsp; &nbsp; audioFileList = mAudioFileDao.getAll();&nbsp; &nbsp; for (AudioFile af: audioFileList) {&nbsp; &nbsp; &nbsp; &nbsp; logAudioFile(af,"After deletion");&nbsp; &nbsp; }logAudioFile方法是: -private void logAudioFile(AudioFile a, String extra) {&nbsp; &nbsp; Log.d(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "AUDIOFILEINFO",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Title is " + a.getTitle() +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; " ID is " + a.getId() +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "\n\tExtra Info is " + extra&nbsp; &nbsp; );}结果(上面的日志):-2019-10-06 11:03:39.757 D/AUDIOFILEINFO: Title is MySong ID is 0&nbsp; &nbsp; &nbsp; &nbsp; Extra Info is Before Insert into DB2019-10-06 11:03:39.808 D/AUDIOFILEINFO: Title is MySong ID is 0&nbsp; &nbsp; &nbsp; &nbsp; Extra Info is Immediately after Insert. ID returned from insert is 12019-10-06 11:03:39.808 D/AUDIOFILEINFO: Title is MySong ID is 1&nbsp; &nbsp; &nbsp; &nbsp; Extra Info is After setting the ID to 12019-10-06 11:03:39.829 D/AUDIOFILEINFO: Title is MySong ID is 1&nbsp; &nbsp; &nbsp; &nbsp; Extra Info is Extracted from DB2019-10-06 11:03:39.831 D/AUDIOFILEINFO: Attempt to delete Audio File undertaken您对房间有何看法?这是征求意见,所以是题外话。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java