简体   繁体   中英

SQLite in Android: Foreign Keys and <table constraint> expected

I have had a search about to try to understand the nature of this problem, but have not had any luck as of yet.

I'm making a RPG-style to-do list to teach myself Android/Java, and I'm making two tables: categories (strength, intelligence etc) and Quests: name, description, EXP etc.

I want each quest to have a category, but I'm getting a table constraints error. My code is below, and the GitHub link is 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');");
}

The error is coming at 'date TEXT', and the error says:

<table constraint> expected, got 'date'.

What's the problem here?

Your issue is that you have mixed up column_constraint syntax with table_constraint syntax ( ie coded the latter where the former should be used ).

You could fix the issue by using

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

As is explained below as is the alternative syntax.

That is the column constraint syntax starts with the REFERENCES .... and is part of the column definition (ie the column name is implicit), whilst table_constraint syntax starts with FORIEGN KEY(column_name) REFERENCES ... and follows the column definitions

So you could have either :-

Column_constraint syntax

  • As part of a column definition

  • category INTEGER NOT NULL REFERENCES categories (id)

eg

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)

or

Table_constraint syntax

  • after the column's have been defined, but still within the column definition ie still inside the brackets.

  • FOREIGN KEY (category) REFERENCES categories (id)

eg

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

You may find column-constraint and table-constraint of use.


date can be a column name. However, I'd suggest that it is wise to not use any SQLite keyword, such as date, as a column name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM