I am having following queries for creating table
Clinic Table :-
private static final String CREATE_TABLE_CLINIC = "CREATE TABLE " + TABLE_CLINIC + " (" + CLINIC_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + CLINIC_NAME + " VARCHAR(255)," + CLINIC_PHONE_NO + " VARCHAR(255)," + CLINIC_LATITUDE + " DOUBLE," + CLINIC_LONGITUDE + " DOUBLE ," + CLINIC_LIKES + " INTEGER ," + STATE + "VARCHAR(255) ," + CITY + " VARCHAR(255)," + AREA + " VARCHAR(255)," + SUB_AREA + " VARCHAR(255)," + PIN_CODE + " INTEGER );";
Doctor Table :-
private static final String CREATE_TABLE_DOCTOR = "CREATE TABLE " + TABLE_DOCTOR + " (" + DOCTOR_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + DOCTOR_NAME + " VARCHAR(255)," + DOCTOR_TYPE + " VARCHAR(255)," + DOCTOR_DEGREE + " VARCHAR(255)," + DOCTOR_EXPERIENCE + " INTEGER ," + DOCTOR_lIKE + " INTEGER );";
Table for many to many relationshipe
doctor_clinic_map :-
private static final String CREATE_TABLE_DOCTOR_CLINIC_MAP = "CREATE TABLE " + TABLE_DOCTOR_CLINIC_MAP + " ( " + DOCTOR_ID + " INTEGER, " + CLINIC_ID + " INTEGER ," + DOCTOR_TIMING + " VARCHAR(255)," + DOCTOR_FEES + " VARCHAR(255),FOREIGN KEY(" + DOCTOR_ID + ") REFERENCES " + TABLE_DOCTOR + "(" + DOCTOR_ID + "),FOREIGN KEY(" + CLINIC_ID + ") REFERENCES " + TABLE_CLINIC + "(" + CLINIC_ID + ") ON UPDATE CASCADE ON DELETE CASCADE );";
I tried to delete one row from clinic table as follows. It is deleting row from clinic as well as doctor_clinic_map table as expected.
public void deleteData(Context context){
String deleteQuery=" delete from clinic where _clinic_id ='1' ";
try {
SQLiteDatabase db = getInstance(context).getReadableDatabase();
db.execSQL(deleteQuery);
} catch (Exception e) {
throw e;
}
}
Now when I tried delete from doctor table for _doctor_id '7' which having corresponding _clinic_id '1' ,it is causing following issue.
android.database.sqlite.SQLiteConstraintException: FOREIGN KEY constraint failed (code 787)
public void deleteData(Context context){
String deleteQuery=" delete from doctor where _doctor_id ='7' ";
try {
SQLiteDatabase db = getInstance(context).getReadableDatabase();
db.execSQL(deleteQuery);
} catch (Exception e) {
throw e;
}
}
In second case 4th row is not deleted.
why constraints fail in second case and not in first case ?
Aucun commentaire:
Enregistrer un commentaire