我一直在试图让外键在我的Android sqlite数据库中工作。我试过下面的语法,但它给了我一个力close:
private static final String TASK_TABLE_CREATE = "create table " + TASK_TABLE + " (" + TASK_ID + " integer primary key autoincrement," + TASK_TITLE + " text not null," + TASK_NOTES + " text not null," + TASK_DATE_TIME + " text not null,FOREIGN KEY ("+TASK_CAT+") REFERENCES "+CAT_TABLE+" ("+CAT_ID+"));";
任何想法我可能做错了什么?如果你需要看到其他表结构,那么我可以,它只是一个非常简单的结构,第二个具有ID和名称。
编辑:
这里是错误:
03-13 13:42:35.389:
ERROR/AndroidRuntime(312): Caused by:
android.database.sqlite.sqliteException:
unknown column “taskCat” in foreign
key definition: create table reminders
(_id integer primary key
autoincrement,task_title text not
null,notes text not null,
reminder_date_time text not null,
FOREIGN KEY (taskCat) REFERENCES
category (_id));
您必须首先定义TASK_CAT列,然后在其上设置外键。
private static final String TASK_TABLE_CREATE = "create table " + TASK_TABLE + " (" + TASK_ID + " integer primary key autoincrement," + TASK_TITLE + " text not null," + TASK_NOTES + " text not null," + TASK_DATE_TIME + " text not null," + TASK_CAT + " integer," + " FOREIGN KEY ("+TASK_CAT+") REFERENCES "+CAT_TABLE+"("+CAT_ID+"));";