0

I have a SQLite database like this below picture where i want to insert value on the same rows of DAYS column. for better understanding I've marked red color where I want to insert value but each time new row is created for same DAYS. How can I achieve this?

enter image description here

Here is my insertData method where I'm inserting value to row

public boolean insertData(String days, String tasbeehName, int count){
    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(COLUMN_DAYS, days);
    contentValues.put(tasbeehName, count);

    //Long result = sqLiteDatabase.insert(TABLE_TASBEEH_COUNT,null, contentValues);
    Long result = sqLiteDatabase.insertWithOnConflict(TABLE_TASBEEH_COUNT,null, contentValues,sqLiteDatabase.CONFLICT_REPLACE);
    if(result == -1)
        return false;
    else
        return true;
}
Al Walid Ashik
  • 1,545
  • 20
  • 32

2 Answers2

0

Not tested but you can try

ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_DAYS, days);
contentValues.put(tasbeehName, count);

sqLiteDatabase.update(TABLE_TASBEEH_COUNT, values, "DAYS = date('now')" , null);
sqLiteDatabase.setTransactionSuccessful();
sqLiteDatabase.endTransaction();
petey
  • 16,914
  • 6
  • 65
  • 97
Jibran Khan
  • 3,236
  • 4
  • 37
  • 50
0

You can first check if a row is already exist with a existing DAYS values then you can update the existing row or insert new row.

You can do this in a single query. Check this answer. I am pasting here sample query from that answer:

INSERT OR REPLACE INTO Employee (id, role, name) 
VALUES (  1, 
        'code monkey',
        (SELECT name FROM Employee WHERE id = 1)
      );

This will update your given columns if a row exist with id = 1 or insert a new row.

Abu Yousuf
  • 5,729
  • 3
  • 31
  • 50