Hierarchical database (?) - SQLite / Android [closed]

0

Good afternoon, I'm starting to study Android and thought of making an application to already study database together. I want to make a "family tree", so that clicking on the name of the person (button) would appear other buttons with the names of their children and so respectively - maybe there are other ways to display this data, I am open to suggestions. >

I made an example of an address book on Android (with buttons to add, delete and list the contacts), I could understand the operation, but I do not know how to apply this to my idea. I researched and discovered the existence of hierarchical databases, and I think it would make sense to use that idea in the application.

But I'm not finding content about it, so if someone could "give me a light" on what to search, or some form of programming that would make it possible to do what I need, it would help a lot.

Thank you in advance.

    
asked by anonymous 26.09.2016 / 22:20

1 answer

1

The link posted in the comment is quite complete, however, I'll put here an example of creating the table.

The simplest way would be to make each record point to its parent (since theoretically every parent is also a child), this approach makes each parent have N children, and each Son can only have one Father which in the case of the family tree is not the case, since each child has two "parents"), in this way, as we have a known number and maximum of parents, we can create two fields for them:

CREATE TABLE pessoa(
id_pessoa INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, 
nome TEXT NOT NULL, 
sobrenome TEXT NOT NULL, 
id_pai INTEGER NOT NULL,
id_mae INTEGER NOT NULL, 
FOREIGN KEY(id_pai) REFERENCES pessoa(id_pessoa),
FOREIGN KEY(id_mae) REFERENCES pessoa(id_pessoa)
);

The bad side of this approach is that SQLite does not support a recursive Query, that is, fetch a record from the person table and join with another record in the same table, so you will not be able to have the information of the parent in the same query as the child (unless you do a subquery, which is a bit more complex for the one who is initiating), then in practice, you would have to find the child, and then do a query with id_pai that you found in the first record.

    
27.09.2016 / 14:47