Part 11 - CATEGORIES
Posted on in Mobile Development
In part 11 of the SQLite quiz tutorial, we will start implementing categories into our quiz. For this we will create another table in our SQLite database that will contain all the categories with a name and id column. We will then connect our questions table to this category table by adding a category_id column, which references a corresponding id in the categories table as a foreign key. This way we can not add any rows to our questions child table, that have a category id that doesn’t also exist in the categories parent table.
By adding “ON DELETE CASCADE” to the schema of our question table, SQLite will automatically delete all questions for a certain category, if we delete the category itself.
We enable foreign key constraints in the onConfigure callback of our SQLiteOpenhelper class, by setting setForeignKeyConstraintsEnabled to true.
After creating our new database schema, we will download the database from Android Studio’s Device File Explorer and open it in “DB Browser for SQLite”. If everything worked correctly, questions with an invalid category id should have not been inserted into the database.
Watch the whole playlist: