SQlite – Android – 外键语法

前端之家收集整理的这篇文章主要介绍了SQlite – Android – 外键语法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在试图让外键在我的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+"));";

更多信息,你可以找到sqlite外键doc

原文链接:https://www.f2er.com/sqlite/198234.html

猜你在找的Sqlite相关文章