2016-08-21 61 views
0

我在數據庫中添加了外鍵。我從我的設備重新安裝了應用程序並增加了數據庫版本。這裏是我的代碼: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 " + ");"; 

回答

2

CREATE TABLETABLE_ZIELE有兩種問題:

  1. 實際上你需要指定您使用的外部列鍵,例如cid INTEGER

  2. 諸如FOREIGN KEY之類的限制條件列在色譜柱規格部分之後。最後移動它們。

+0

第一個是錯誤的,因爲它的外鍵。而第二個沒有幫助 – XxGoliathusxX

+0

你需要他們兩個。表約束不聲明列,並且不能在列規範之間混合表約束。 – laalto

+0

看編輯 – XxGoliathusxX

2

您必須實施FOREIGN KEY作爲表或列約束。在您選擇的第一種情況下,您需要引用您之前定義的列。

documentation你找到了一些正確的做法。

在你的情況下,你需要使用列約束,而不是像你打算做的那樣。

您只需更換線,如

"FOREIGN KEY(" + COLUMN_K_ID + ") REFERENCES " + TABLE_ZIELE_KOERPER + "(" + COLUMN_ID + ") ON DELETE CASCADE, " + 

COLUMN_K_ID + " REFERENCES " + TABLE_ZIELE_KOERPER + "(" + COLUMN_ID + ") ON DELETE CASCADE, " + 
+0

我不明白。爲什麼我應該刪除那個FOREIGN KEY(...)? – XxGoliathusxX

+0

因爲這就是你如何定義一個引用外部表的列。 'FOREIGN KEY'僅用於表約束 – tynn