How can I check whether data exist in Room database before inserting into database on Android?



In my application I want to use Room Database to show offline data.
I wrote below code, but when clicked on button to add in data, if this data exist in the Room database it shows ForceClose error!
How can I check for this data in the database, before adding to the database?

My NewsOfflineDatum class :

@Entity(tableName = "newsList")
public class NewsOfflineDatum {
    @PrimaryKey
    private int id;

    @ColumnInfo(name = "newsTitle")
    private String title;

    @ColumnInfo(name = "newsImage")
    private String image;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }
}

My adapter class :

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.MyViewHolder> {

    private List<Today> model;
    private Context context;

    public NewsAdapter(List<Today> model) {
        this.model = model;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_news, parent, false);
        context = parent.getContext();

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        final Today todayModel = model.get(position);

        Glide.with(context)
                .load(AppConstant.IMAGE_URL + todayModel.getImage())
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(holder.rowNews_img);
        holder.rowNews_txt.setText(todayModel.getTitle());
        holder.rowNews_dl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NewsOfflineDatum offlineDatum = new NewsOfflineDatum();
                offlineDatum.setId(todayModel.getId());
                offlineDatum.setTitle(todayModel.getTitle());
                offlineDatum.setImage(todayModel.getImage());

                MainActivity.myAppDatabase.getMyDao().addNews(offlineDatum);

                Toast.makeText(context, "Saved :)", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return model.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.rowNews_img)
        ImageView rowNews_img;
        @BindView(R.id.rowNews_txt)
        TextView rowNews_txt;
        @BindView(R.id.rowNews_dl)
        ImageView rowNews_dl;

        public MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}

LogCat error :

Process: com.example.mac8.mvplearnclicksite, PID: 7697
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: newsList.id (code 1555)
#################################################################
Error Code : 1555 (SQLITE_CONSTRAINT_PRIMARYKEY)
Caused By : Abort due to constraint violation.
    (UNIQUE constraint failed: newsList.id (code 1555))
#################################################################
    at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
    at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:865)
    at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
    at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
    at android.arch.persistence.db.framework.FrameworkSQLiteStatement.executeInsert(FrameworkSQLiteStatement.java:50)
    at android.arch.persistence.room.EntityInsertionAdapter.insert(EntityInsertionAdapter.java:64)
    at com.example.mac8.mvplearnclicksite.Data.DataBase.MyDao_Impl.addNews(MyDao_Impl.java:49)
    at com.example.mac8.mvplearnclicksite.Home.NewsAdapter$1.onClick(NewsAdapter.java:59)
    at android.view.View.performClick(View.java:6261)
    at android.view.View$PerformClick.run(View.java:23752)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6776)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1518)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)

Shows me error for this line : MainActivity.myAppDatabase.getMyDao().addNews(offlineDatum);

How can I fix this?

Answer

Update your query with this annotation :

@Insert(onConflict = OnConflictStrategy.REPLACE)


Source: stackoverflow