【问题标题】:SQLite in Android: Foreign Keys and <table constraint> expectedAndroid 中的 SQLite:需要外键和 <table constraint>
【发布时间】:2018-04-24 04:31:48
【问题描述】:

我已经进行了一次搜索,试图了解这个问题的性质,但到目前为止还没有任何运气。

我正在制作一个 RPG 风格的待办事项列表来自学 Android/Java,我正在制作两个表格:类别(力量、智力等)和任务:名称、描述、EXP 等。

我希望每个任务都有一个类别,但我遇到了表格约束错误。我的代码如下,GitHub链接是here

public void onCreate(SQLiteDatabase db){
    setForeignKeyConstraintsEnabled(db);

db.execSQL("CREATE TABLE " + CATEGORY_TABLE_NAME + "(id INTEGER primary key autoincrement NOT NULL, name TEXT, exp INTEGER, level INTEGER )");
db.execSQL("CREATE TABLE " + QUEST_TABLE_NAME + "(id INTEGER primary key autoincrement NOT NULL, name TEXT, description TEXT, expValue INTEGER, category INTEGER NOT NULL, FOREIGN KEY (category) REFERENCES categories (id), date TEXT");

db.execSQL("INSERT INTO " + CATEGORY_TABLE_NAME + "('name', 'exp', 'level') VALUES ('Strength', 0, 0 );");
db.execSQL("INSERT INTO " + CATEGORY_TABLE_NAME + "('name', 'exp', 'level') VALUES ('Stamina', 0, 0 );");
db.execSQL("INSERT INTO " + CATEGORY_TABLE_NAME + "('name', 'exp', 'level') VALUES ('Intelligence', 0, 0 );");
db.execSQL("INSERT INTO " + CATEGORY_TABLE_NAME + "('name', 'exp', 'level') VALUES ('Social', 0, 0 );");
db.execSQL("INSERT INTO " + QUEST_TABLE_NAME + "('name', 'description', 'expValue', 'category', 'date') VALUES ('Gym', 'Weightlifting down The Gym', '300', '1', '25/05/2017');");
}

错误出现在“date TEXT”,错误显示:

<table constraint> expected, got 'date'.

这里有什么问题?

【问题讨论】:

  • 试着把它(日期文本)放在外键之前我不确定,但我认为你需要把所有列放在外键之前
  • @crammeur 如果我在 FOREIGN KEY 位之前移动日期,我会收到以下错误:')'、DEFERRABLE、MATCH、NOT、ON 或预期逗号,文件意外结束
  • 所以将日期更改为 date1 因为日期是函数
  • 你忘了 ) 在字符串的末尾
  • 我得到了你的建议!我用过: db.execSQL("CREATE TABLE " + QUEST_TABLE_NAME + "(id INTEGER 主键 autoincrement NOT NULL, name TEXT, description TEXT, expValue INTEGER, date TEXT, category INTEGER NOT NULL, FOREIGN KEY (category) REFERENCES '+CATEGORY_TABLE_NAME+ ' (id))");

标签: java android mysql sqlite


【解决方案1】:

您的问题是您将 column_constraint 语法与 table_constraint 语法混为一谈(即在应该使用前者的地方编码了后者)。

你可以通过使用来解决这个问题

db.execSQL("CREATE TABLE " + QUEST_TABLE_NAME + "(id INTEGER primary key autoincrement NOT NULL, name TEXT, description TEXT, expValue INTEGER, category INTEGER NOT NULL REFERENCES categories (id), date TEXT");

如下所述,替代语法也是如此。

即列约束语法以 REFERENCES .... 开头并且是列定义的一部分(即列名是隐式的),而 table_constraint 语法以 FORIEGN KEY(column_name) REFERENCES ... 开头并且遵循列定义

所以你可以有:-

Column_constraint 语法

  • 作为列定义的一部分

  • category INTEGER NOT NULL REFERENCES categories (id)

例如

CREATE TABLE yourtablename (id INTEGER primary key autoincrement NOT NULL, name TEXT, description TEXT, expValue INTEGER, category INTEGER NOT NULL REFERENCES categories (id), date TEXT)

Table_constraint 语法

  • 在定义列之后,但仍在列定义内,即仍在括号内。

  • FOREIGN KEY (category) REFERENCES categories (id)

例如

CREATE TABLE yourtablename (id INTEGER primary key autoincrement NOT NULL, name TEXT, description TEXT, expValue INTEGER, category INTEGER NOT NULL, date TEXT, FOREIGN KEY (category) REFERENCES categories (id));

您可能会发现column-constrainttable-constraint 的使用。


日期 可以是列名。但是,我建议不要使用任何 SQLite 关键字(例如日期)作为列名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-13
    • 2012-09-02
    • 2011-03-04
    • 2011-07-14
    • 2012-12-25
    • 2012-06-28
    相关资源
    最近更新 更多