我在數據庫中添加了外鍵。我從我的設備重新安裝了應用程序並增加了數據庫版本。這裏是我的代碼:Android Sqlite外鍵Unknwon列
恆:private static final String COLUMN_K_ID = "kid";
創建表:
private static final String CREATE_TABLE_ZIELE_KOERPER = "CREATE TABLE " + TABLE_ZIELE_KOERPER + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_OPTION + " INTEGER, " +
COLUMN_TENDENZ + " INTEGER);";
private static final String CREATE_TABLE_ZIELE_CARDIO = "CREATE TABLE " + TABLE_ZIELE_CARDIO + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_EXTRA + " TEXT);";
private static final String CREATE_TABLE_ZIELE_TRAINING = "CREATE TABLE " + TABLE_ZIELE_TRAINING + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_EXTRA + " TEXT, " +
COLUMN_MUSKEL + " TEXT, " +
COLUMN_WIEDERHOLUNGEN + " INTEGER)";
private static final String CREATE_TABLE_ZIELE = "CREATE TABLE " + TABLE_ZIELE + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
"FOREIGN KEY(" + COLUMN_K_ID + ") REFERENCES " + TABLE_ZIELE_KOERPER + "(" + COLUMN_ID + ") ON DELETE CASCADE, " +
"FOREIGN KEY(" + COLUMN_C_ID + ") REFERENCES " + TABLE_ZIELE_CARDIO + "(" + COLUMN_ID + ") ON DELETE CASCADE, " +
"FOREIGN KEY(" + COLUMN_T_ID + ") REFERENCES " + TABLE_ZIELE_TRAINING + "(" + COLUMN_ID + ") ON DELETE CASCADE, " +
COLUMN_NAME + " TEXT, " +
COLUMN_ERSTELLDATUM + " LONG, " +
COLUMN_DEADLINE + " LONG, " +
COLUMN_DONE + " INTEGER, " +
COLUMN_VALUE + " FLOAT, " +
COLUMN_FORTSCHRITT + " FLOAT " +
");";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_ZIELE_KOERPER);
db.execSQL(CREATE_TABLE_ZIELE_CARDIO);
db.execSQL(CREATE_TABLE_ZIELE_TRAINING);
db.execSQL(CREATE_TABLE_ZIELE);
}
登錄:
未知列 「孩子」 外鍵的定義(代碼1):在編譯CREATE TABLE ziele(id INTEGER PRIMARY KEY AUTOINCREMENT,FOREIGN KEY(kid)REFERENCES koerperziele(id)ON DELETE CASCADE,FOREIGN KEY(cid)REFERENCES cardioziele(id)ON DELETE CASCADE,FOREIGN KEY(t_id)REFERENCES trainingsziele(id)ONDELETE CASCADE,名稱TEXT,erstelldatum LONG,截止期限LONG,完成INTEGER,值FLOAT,fortschritt FLOAT);
激活外鍵:
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
db.execSQL("PRAGMA foreign_keys=ON");
}
編輯:還沒工作
private static final String CREATE_TABLE_ZIELE = "CREATE TABLE " + TABLE_ZIELE + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_NAME + " TEXT, " +
COLUMN_ERSTELLDATUM + " LONG, " +
COLUMN_DEADLINE + " LONG, " +
COLUMN_DONE + " INTEGER, " +
COLUMN_VALUE + " FLOAT, " +
COLUMN_FORTSCHRITT + " FLOAT, " +
"FOREIGN KEY(" + COLUMN_K_ID + " INTEGER) REFERENCES " + TABLE_ZIELE_KOERPER + "(" + COLUMN_ID + ") ON DELETE CASCADE, " +
"FOREIGN KEY(" + COLUMN_C_ID + " INTEGER) REFERENCES " + TABLE_ZIELE_CARDIO + "(" + COLUMN_ID + ") ON DELETE CASCADE, " +
"FOREIGN KEY(" + COLUMN_T_ID + " INTEGER) REFERENCES " + TABLE_ZIELE_TRAINING + "(" + COLUMN_ID + ") ON DELETE CASCADE " + ");";
第一個是錯誤的,因爲它的外鍵。而第二個沒有幫助 – XxGoliathusxX
你需要他們兩個。表約束不聲明列,並且不能在列規範之間混合表約束。 – laalto
看編輯 – XxGoliathusxX