Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.8k views
in Technique[技术] by (71.8m points)

sqlite - Android Room: Dynamically deciding on what tables to search

I have a Room Database with a Questions-Table and multiple Answer-Tables (with different kind of answers, like Multiple Choice etc).

I want to query multiple Questions and their corresponding Answers. Each Question has an attribute "type" that tells me what Table contains its Answers.

Would it be more efficient to look through all Answer-Tables or use 2 Queries, one to get the Questions and then a Query that searches the correct Answer-Table, based on the type-attribute? I assume some of the Answer-tables could get around 10s to 100s of entries.

Alternatively, could I somehow decide on which Answer-Table to search in the Query itself (based on the type-attribute)?

Thanks in advance.

Edit:

@Query("SELECT * FROM questions_table WHERE dataSet = :dataSetId")
    LiveData<List<QuestionWithAnswers>> getQuestionsWithAnswers(int dataSetId);
public class QuestionWithAnswers extends Question {
    @Relation(parentColumn = "id", entityColumn = "question")
    public List<MultipleChoiceAnswer> mcAnswers;

    @Relation(parentColumn = "id", entityColumn = "question")
    public NumberRangeAnswer rangeAnswer;
}

This would search both Tables (MultipleChoiceAnswer and NumberRangeAnswer) but only one would ever actually contain values. My question is if there's a better way to do this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...