SQLite的 - 机器人 - 外键语法 [英] SQlite - Android - Foreign key syntax

查看:296
本文介绍了SQLite的 - 机器人 - 外键语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图让我在Android的SQLite数据库的工作外键。我曾尝试下面的语法,但它给了我一个强制关闭:

 私有静态最后弦乐TASK_TABLE_CREATE =创建表
            + TASK_TABLE +(+ TASK_ID
            +整数主键自动增量,+ TASK_TITLE
            +文本不为空,+ TASK_NOTES +文字不为空,
    + TASK_DATE_TIME +文字不为空,外键(+ TASK_CAT +)参考+ CAT_TABLE +(+ CAT_ID +));;
 

任何想法我可能是做错了什么?如果你需要看到其他的表结构,那么我就可以了,它只是一个非常简单的结构,第二个有一个ID和名称。

编辑:

下面是错误:

  

03-13 13:42:35.389:   ERROR / AndroidRuntime(312):产生的原因:   android.database.sqlite.SQLiteException:   在国外未知列taskCat   键定义:CREATE TABLE提醒   (_id整数主键   自动增量,task_title文字不   空,笔记文本不为空,   reminder_date_time文字不为空,   外键(taskCat)参考   类别(_id));

解决方案

您需要,然后再定义你的 TASK_CAT 列设置为外键就可以了。

 私有静态最后弦乐TASK_TABLE_CREATE =创建表
        + TASK_TABLE +(
        + TASK_ID +整数主键自动增量,
        + TASK_TITLE +文字不为空,
        + TASK_NOTES +文字不为空,
        + TASK_DATE_TIME +文字不为空,
        + TASK_CAT +整数
        +外键(+ TASK_CAT +)参考+ CAT_TABLE +(+ CAT_ID +));;
 

更多信息,你可以找到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+"));";

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.

Edit:

Here is the error:

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));

解决方案

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+"));";

More information you can find on sqlite foreign keys doc.

这篇关于SQLite的 - 机器人 - 外键语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆