mercredi 14 janvier 2015

Android SQL Foreign Keys SQLitedatabase manipulation

I'm having a few issues with my SQLite.


I have 3 tables Question, Category and Question_Category. I want to make a normalised database - where each question can have multiple categories.


I think I'm going about it the right way - but a little guidance would be nice.



CREATE TABLE QUESTION (ID INTEGER PRIMARY KEY AUTOINCREMENT,
Question VARCHAR2(50),
Answer VARCHAR2(50),
Completed INTEGER(1) DEFAULT 0);

CREATE TABLE CATEGORY (ID INTEGER PRIMARY KEY AUTOINCREMENT,
Category VARCHAR2(50));

CREATE TABLE QUESTION_CATEGORY (QuestionID INTEGER,
CategoryID INTEGER,
FOREIGN KEY(QuestionID) REFERENCES QUESTION(ID),
FOREIGN KEY(CategoryID) REFERENCES CATEGORY(ID));


The second part is, if that is indeed a good way of setting up my tables - how can I insert items into the database using SQLiteDatabase? I've looked online, but I can't find a tutorial that explains how to insert when there's foreign keys involved.


TLDR:



  1. Is that a good way of setting up my tables?

  2. How can I do inserts with foreign keys + SQLDatabase


Aucun commentaire:

Enregistrer un commentaire