use*_*940 51 sqlite android foreign-keys
我一直在尝试在我的Android SQLite数据库中使用外键.我尝试了以下语法,但它给了我一个力量关闭:
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+"));";
Run Code Online (Sandbox Code Playgroud)
我有什么想法可能做错了吗?如果您需要查看其他表结构,那么我可以,它只是一个非常简单的结构,第二个带有ID和名称.
编辑:
这是错误:
03-13 13:42:35.389:ERROR/AndroidRuntime(312):引起:android.database.sqlite.SQLiteException:外键定义中的未知列"taskCat":创建表提醒(_id整数主键自动增量,task_title文本没有null,notes text not null,reminder_date_time text not null,FOREIGN KEY(taskCat)REFERENCES category(_id));
jet*_*hro 107
您必须先定义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+"));";
Run Code Online (Sandbox Code Playgroud)
您可以在sqlite外键doc上找到更多信息.
nom*_*mer 10
由于我无法评论,除了@jethro之外还添加了这个注释.
我发现你还需要将FOREIGN KEY行作为创建表语句的最后一部分,否则在安装应用程序时会出现语法错误.我的意思是,你不能做这样的事情:
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_CAT + " integer,"
+ " FOREIGN KEY ("+TASK_CAT+") REFERENCES "+CAT_TABLE+" ("+CAT_ID+"), "
+ TASK_DATE_TIME + " text not null);";
Run Code Online (Sandbox Code Playgroud)
我把TASK_DATE_TIME放在外键行之后.
归档时间: |
|
查看次数: |
98346 次 |
最近记录: |