问题描述
我一直在尝试让外键在我的 Android SQLite 数据库中工作.我尝试了以下语法,但它给了我一个强制关闭:
I've been trying to get foreign keys working within my Android SQLite database. I have tried the following syntax but it gives me a force 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 和名称的第二个.
Any ideas what I might be doing wrong? if you need to see the other table structure then I can, its just a very simple structure for the second with an ID and a name.
这是错误:
03-13 13:42:35.389:错误/AndroidRuntime(312):由以下原因引起:android.database.sqlite.SQLiteException:国外未知列taskCat"关键定义:创建表提醒(_id 整数主键自动增量,task_title 文本不是null,注释文本不为空,提醒日期时间文本不为空,外键(taskCat)参考类别 (_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
列,然后在上面设置外键.
You have to define your TASK_CAT
column first and then set foreign key on it.
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+"));";
您可以在 sqlite 外键 doc 上找到更多信息.
More information you can find on sqlite foreign keys doc.
这篇关于SQlite - Android - 外键语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!