SQLite in Android: Foreign Keys and expected
前端 未结 1 765
闹比i
闹比i 2021-01-24 09:16

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 Androi

相关标签:
1条回答
  • 2021-01-24 09:41

    Your issue is that you have mixed up column_constraint syntax with table_constraint syntax (i.e 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 (i.e. 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)

    e.g.

    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 i.e. still inside the brackets.

    • FOREIGN KEY (category) REFERENCES categories (id)

    e.g

    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.

    0 讨论(0)
提交回复
热议问题