Android Sqlite外键Unknwon专栏

时间:2016-08-21 20:06:58

标签: android sqlite exception key

我在我的数据库中添加了外键。我从我的设备重新安装了该应用程序并增加了数据库版本。这是我的代码:

常数: 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); }

创建表格:

 @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        db.execSQL("PRAGMA foreign_keys=ON");
    }

日志:

外键定义中的

未知列“kid”(代码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)ON DELETE CASCADE,name TEXT,erstelldatum LONG,截止日期LONG,完成INTEGER,值FLOAT,fortschritt FLOAT); < / p>

激活外键:

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  " + ");";

编辑:仍然无法正常工作

readCSV :: FilePath -> IO [[(Text,Text)]]
readCSV filename = do
  headers : body <- T.lines <$> I.readFile filename
  return $ map (Prelude.zip (splitOn "," headers) . splitOn ",") body

2 个答案:

答案 0 :(得分:2)

CREATE TABLE TABLE_ZIELE有两种问题:

  1. 您需要实际指定用作外键的列,例如cid INTEGER

  2. FOREIGN KEY之类的约束属于列规范部分。最后移动它们。

答案 1 :(得分: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, " +