0

I am busy with a read-only book reader app that has one database and 3 tables. One table called chapters where the chapters have names, one table called subchapters where the chapters are numbers, and one table called entries which is the text content of the subchapters table. Each entry will have a number too for better organizatio.

chapters table has two columns: id, and name

subchapters table has two columns: id, and number

entries table has two columns: id, and text

I have generated the rawquery to pull a random text entry from the entries table on activity start and it works fine. However, I need two things:

  1. When the random text displays on the activity screen in the view, I need it to give not just the text but the chapter name and the subchapter number as well.

  2. When that view is clicked, I need it to take the user to the subchapter that this text came from where the user can then read and/or navigate, etc..

Problem I am having is that I need the app to be able to know which chapter and subchapter and entry that random text entry came from so it can display it and then take the user there on click.

I know it has to with JOIN and I have watched many tutorials and read many posts on here but I haven't seen anything like what I am talking about yet or I am just not understanding it seeing other unrelated examples.

This is my current rawQuery to get the random text entry:

public List<String> getRandomTextEntry() {
    List<String> list = new ArrayList<>();
    Cursor cursor = database.rawQuery("SELECT * FROM entries ORDER BY 
RANDOM() LIMIT 1", null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        list.add(cursor.getString(1));
        cursor.moveToNext();
    }
    cursor.close();
    return list;
}

Any help with this is greatly appreciated.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Stark
  • 107
  • 1
  • 11
  • 1
    Hi. Please read & act on [mcve]. We don't want to type code in an answer that an asker didn't want to even cut & paste. [Is there any rule of thumb to construct SQL query from a human-readable description?](https://stackoverflow.com/a/33952141/3404097) The tables you describe do not tell you what chapter or subchapter an entry is in so they cannot answer your query. Time to read a textbook on information modeling, the relational model & database design. (Nb--A manual for a design tool, api or language to record designs is not a manual/tutorial on design.) PS "chapter C subchapter S entry is E" – philipxy Oct 08 '18 at 21:05
  • @philipxy I'm not 100% sure what you meant by that. I copied and pasted what I thought would help but I see your point that you can't answer what you can't see. I will take a look at the resources that you have pointed out to me and I appreciate your time and your reply. – Stark Oct 08 '18 at 21:30
  • Re "The tables you describe do not tell you what chapter or subchapter an entry is in so they cannot answer your query."--How would a *person* answer that from the 3 tables? (Rhetorical. They can't.) My MSCE comment is distinct from that. Also I meant to put the link re querying last as a 1-answer design resource. – philipxy Oct 08 '18 at 21:51
  • @philipxy thank you I do appreciate it – Stark Oct 08 '18 at 22:12
  • 1
    I expanded my comment into an answer. – philipxy Oct 08 '18 at 22:16

2 Answers2

1

Why are you want to use multiple tables? In my opinion, it is better to have only one table with the following columns: id, chapter, subchapter, and text. Of course, you have to use the same chapter name and maybe the same subchapter numbers multiple times but then you do a text column query and in that case, at the same time, you can get which chapter it is and which subchapter. This tutorial describes really good how to handle multiple columns: https://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

But if you still want to use multiple tables and "link" them, I would suggest following this tutorial: https://www.androidhive.info/2013/09/android-sqlite-database-with-multiple-tables/ It is basically doing exactly the same what you want.

tikiss
  • 48
  • 1
  • 8
  • thank you. I actually thought about transferring it to one table. It would make it a lot more straight forward. I am going to look into it. Also, I have already read both of the tutorials that you referred me to. However, thank you for referring them to me because they are good. I just have to up my knowledge of database in general. I appreciate your time and your reply. – Stark Oct 08 '18 at 21:32
  • @Stark & tikiss Principles of good design (specifically normalization) say to have Chapters & Subchapters separately from a table like "chapter C subchapter S has entry E text TEXT" to avoid the problematic duplication of names & numbers. – philipxy Oct 09 '18 at 01:40
1

I need the app to be able to know which chapter and subchapter and entry that random text entry came from

The tables you describe do not tell you what chapter or subchapter an entry is in so they cannot answer your query.

PS

You could have a base table CSE holding the rows where "chapter C subchapter S entry is E". Then you can search for text using the table for rows where "chapter C subchapter S entry is ID AND entry ID text is TEXT AND E=ID", which is select * from CSE inner join Entries on E=ID. But: CSE & Chapters etc can't contain just any values. Eg Chapters IDs are unique, ie count(select ID from Chapters) = count(Chapters)--a superkey (SQL UNIQUE NOT NULL) constraint, candidate for a SQL PK (primary key) constraint. Eg CSE C values must be Chapters ID values, ie select C from CSE <= select ID as C from Chapters--part of a FK (foreign key) constraint. Eg if there are no empty chapters then select C from CSE >= select ID as C from Chapters.

Or you could have base tables CS "chapter C contains subchapter S" & SE "subchapter S contains entry E", so CSE = CS natural join SE. Unfortunately that requires a lot of other constraints that come down to saying that (surprise) CSE = CS natural join SE.

You don't want to use just one base table instead of CSE & the other 3 because it complicates constraining that every time a chapter id appears it is with the same name & every time a subchapter id appears it is with the same number.

This doesn't mean that CSE + your 3 tables is the best design or a good design. I'm just illustrating that you need to find table row membership conditions (table (characteristic) predicates) & corresponding base tables necessary & sufficient to describe your business situations & that choices have repercussions.

Time to read a textbook on information modeling, the relational model & database design. (Nb--A manual for a design tool, API or language to record designs is not a manual/tutorial on design.)

philipxy
  • 14,867
  • 6
  • 39
  • 83