I have the following table structure:
Table Days: [_id,date,name]
^
|
Table Events: [_id, day_id ,description]
The foreign key day_id is set to ON DELETE CASCADE
I want to "insert or update" the day 2. So I make:
ContentValues values= new ContentValues();
values.put("_id",2);
values.put("date,"...");
values.put("name","welcome");
mDb.replace("days",null,values);
The problem is that if the day_id=2 already exists (in my case is the most probably option) the DBengine deletes the table and inserts a new row so all the events associated to that day are also deleted.
This solution works but its very sub-optimal right?
try {
mDb.insertOrThrow("days", null, values);
} catch (Exception e) {
mDb.update("days", values, "_id=2", null);
}
Whats the correct sollution for that problem