enter image description hereI am trying to either update(if possible) or insert my database in android.I have implemented my code but it's not working as i wanted.As i am new to android, there might be mistake in my code.So the scenario, is there are some items with some quantity,now when i click "ADD TO CART" button this item should be inserted in my database and now when quantity of item is changed then i want to just update my database.But my code always insert a item in new row in the database.Here's my code snippet
addcart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(Integer.parseInt(itemquant.getText().toString())>0){
SQLiteDatabase db=cDBHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(CartContract.CartEntry.CART_NAME,t1.getText().toString());
values.put(CartContract.CartEntry.CART_PRICE,Integer.parseInt(t2.getText().toString()));
values.put(CartContract.CartEntry.CART_QUANTITY,Integer.parseInt(itemquant.getText().toString()));
int id=(int)db.insertWithOnConflict(CartContract.CartEntry.TABLE_NAME,null,values,SQLiteDatabase.CONFLICT_IGNORE);
if(id==-1)
{
db.update(CartContract.CartEntry.TABLE_NAME,values, CartContract.CartEntry.CART_NAME+"=?",new String[] {t1.getText().toString()});
}
Toast.makeText(MainActivity.this,String.valueOf(id),Toast.LENGTH_LONG).show();}
}
});