SQlite - Android - Foreign key syntax(SQlite - Android - 外键语法)
问题描述
我一直在尝试让外键在我的 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 - 外键语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:SQlite - Android - 外键语法
- UITextView 内容插图 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- URL编码Swift iOS 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01