I have 4 tables in my SQLite database. One with questions, one with answers with question id foreign key, one category table, and one question-category table that holds a many-to-many relationship between the questions and categories (Each question can belong to multiple categories).
So, in my Android application I want to extract one question at the time, with all the data belonging to it. I need help with how to handle it properly. Should I make one method in my Data Access Object which is named something like getQuestionSet(int questionId) that delegates further method invocations to the question table, answer table etc. with the questionId as a where-clause? Or should I make a view that already has this sql-query saved? But then will have several rows containing the question-text because there are 4 answers belonging to each question. Or are there other solutions?
Also, if I need to do the first approach, do I make an Data Transfer Object that carries Cursor references around?
Always try to perform a single query on the database whenever possible, it is much faster and requires less effort to manage. You can use an INNER JOIN operation to get both the question and answers in a single Cursor. The result might contain duplicate data but there is no harm in that. Your result cursor might look like:
You know that in this query the question text is always the same so you can just read it from the first row and ignore the others.
Making a view or not is up to you, but it probably isn’t necessary.