Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.2k 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...