我遇到了这个奇怪的问题,要点是,当我单击电影时,我将使用Android-room和Dao将数据插入本地数据库。但是,仅插入特定时间的作品(有时一次,有时最多八个),此后无论我单击影片(不同的影片)多少次,它都将不再插入数据库。我的程序没有错误,我尝试对其进行调试,每次单击都将运行插入操作,但是新数据将不再显示在数据库中。
这是我给Dao的代码,很标准。
@Dao
public interface MovieDao {
@Query("SELECT * FROM movie ORDER BY title")
LiveData<List<MovieEntry>> loadAllTasks();
@Insert(onConflict = OnConflictStrategy.REPLACE)
void insertTask(MovieEntry taskEntry);
@Update(onConflict = OnConflictStrategy.REPLACE)
void updateTask(MovieEntry movieEntry);
@Query("DELETE FROM movie where title= :title")
void deleteTask(String title);
@Query("SELECT * FROM movie WHERE id = :id")
LiveData<MovieEntry> loadMovieById(int id);
@Query("DELETE FROM movie")
void deleteALL();
}
这是我的代码,每次电影点击时都会运行
appExecutors.getInstance().diskIO().execute(new Runnable() {
@Override
public void run() {
movieDatabase.movieDao().insertTask(movie_obj);
LogUtil.d(LOG_TAG,"INSERTING INTO DATABASE");
}
});
一切都很标准,没有错误... 我正在使用Android调试数据库来检查“我的数据库”上的更新。
这是我的实体实现,我有所有的设置者和获取者 但是在这里展示它会浪费空间
@Entity(tableName = "movie")
public class MovieEntry {
@PrimaryKey(autoGenerate = true)
private int id;
private String title;
private String posterURL;
private String topRate;
private String popularity;
private String language;
private Boolean favorite;
private String releaseDate;
private String overview;
private String sortBy;
//This constructor writes to the database, so it does not know what the id would be
@Ignore
public MovieEntry(String title, String posterURL, String topRate,
String popularity, String language, Boolean favorite, String
releaseDate, String overview,String sortBy) {
this.title = title;
this.posterURL = posterURL;
this.topRate = topRate;
this.popularity = popularity;
this.language = language;
this.favorite = favorite;
this.releaseDate = releaseDate;
this.overview = overview;
this.sortBy=sortBy;
}
// when read from the database then id would be required
public MovieEntry(int id,String title, String posterURL, String
topRate, String popularity, String language, Boolean favorite,
String releaseDate, String overview,String sortBy) {
this.id = id;
this.title = title;
this.posterURL = posterURL;
this.topRate = topRate;
this.popularity = popularity;
this.language = language;
this.favorite = favorite;
this.releaseDate = releaseDate;
this.overview = overview;
this.sortBy=sortBy;
}