2012-10-24 3 views
2

나는 SqliteConstraintException있는 proble 있습니다. 내 DB가SqliteConstraintException 안드로이드

private static final String CREATE_TABLE_1 = 
    "create table table_1 (uid integer primary key autoincrement, " 
+ table_1.c1 + " TEXT," 
+ table_1.c2 + " integer," 
+ table_1.c3 + " TEXT," 
+ table_1.c4 + " TEXT," 
+ table_1.c5 + " integer);"; 

private static final String CREATE_TABLE_2 = 
     "create table table_2 (uid integer primary key autoincrement, " 
+ table_2.c1 + " TEXT," 
+ table_2.c2 + " integer," 
+ table_2.c3 + " TEXT);"; 

private static final String CREATE_TABLE_3 = 
     "create table table_3 (uid integer primary key autoincrement, " 
     + "uid_table_1 integer, " 
     + "uid_table_2 integer, " 

+ "FOREIGN KEY(uid_table_1) REFERENCES table_1(uid) ON DELETE CASCADE, " 
+ "FOREIGN KEY(uid_table_2) REFERENCES table_2(uid) ON DELETE CASCADE);"; 

private static final String CREATE_TABLE_4 = 
     "create table table_4 (uid integer primary key autoincrement, " 
+ "uid_table_2 integer," 
+ "FOREIGN KEY(uid_table_2) REFERENCES table_2(uid) ON DELETE CASCADE);"; 

처럼 그리고 내가 외래 키 제약

@Override 
public void onOpen(SQLiteDatabase db) { 
super.onOpen(db); 
if (!db.isReadOnly()) { 
// Enable foreign key constraints 
db.execSQL("PRAGMA foreign_keys=ON;"); 
} 

수 있도록 내 SQLiteOpenHelper에 사용하지만 난이 table_2의 행을 삭제할 때, 내가 SqliteConstraintException를 얻을. 제발 도와 주실 수 있습니까?

고맙습니다.

답변

0

대부분의 테이블이 서로를 참조하는 것처럼 보입니다. 다른 테이블에서 외래 키로 사용되는 표 2의 행을 삭제하려면이를 참조하는 레코드를 삭제해야합니다. 그래서 2에서 삭제하려고하는 행에 대한 외래 키 참조가있는 테이블 3과 4의 레코드를 삭제 한 다음 해당 행에 대한 삭제를 호출합니다.

+0

답변 해 주셔서 감사합니다. – Hasina

+0

그것이 당신을 위해 작동한다면 받아들이는 것을 잊지 마라. – toadzky

+0

그래서 나는 그것을한다. mDb.beginTransaction(); \t \t { \t \t \t mDb.delete ("table_3", "uid_table_2 ="+ uid, null); \t \t \t mDb.delete ("table_4", "uid_table_2 ="+ uid, null); \t \t \t this.mDb.delete ("table_2", "uid ="+ uid, null); \t \t} 캐치 (예외 E) { \t \t \t \t \t } {마지막 \t \t \t mDb.endTransaction(); \t \t} ' – Hasina