java - Android 中的 SQLite : Foreign Keys and <table constraint> expected

标签 java android mysql sqlite

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

我正在制作一个 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');");
}

错误出现在“日期 TEXT”,错误内容为:

<table constraint> expected, got 'date'.

这里有什么问题?

最佳答案

您的问题是您混淆了 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), 日期文本");

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

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

所以你可以有:-

Column_constraint 语法

  • 作为列定义的一部分

  • category INTEGER NOT NULL REFERENCES 类别 (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的用途。


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

关于java - Android 中的 SQLite : Foreign Keys and <table constraint> expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47231353/

相关文章:

java.sql.SQLException : Column count doesn't match value count at row 1 异常

mysql - 调试约 39 分钟后关闭的 MySQL 连接的最佳方法?

java - 需要指导来选择正确的方式来捕获相机输入

android - 禁用自动关闭应用程序 Android

java - HashSet 与 ArrayList contains() 几个小型 String 集合的性能

带有谷歌地图的Android应用程序,在 Release模式下工作,但上传到市场时不工作

android - ListView 中的 ViewPager 和 OnItemClickListener

php - CodeIgniter 无法回显日期时间结果

java - 尽管导入类也没有得到建议,但在 Media.Type 中出现错误

java - 如何用java从网站上检索单条数据?