Hierarchical database(?) - Sqlite/Android

Asked

Viewed 99 times

0

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

I made an example of a calendar of contacts on Android (with buttons to add, delete and list contacts), I was able to understand how it works, but I don’t 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 this idea in the application.

However, I’m not finding any content on this, so if someone could "give me some light" on what to research, or some form of programming that makes it possible to do what I need, it would help a lot.

Thank you in advance.

  • 2

    See if this solves for you: http://answall.com/q/2425/101

  • Hello. Welcome to SOPT. If you haven’t done it yet, do the [tour] and read [Ask]. This site is not a forum, and so your question would need to be a little more specific. In addition to reading the link already suggested to you, you can [Dit] the question to focus on some aspect of your problem (for example, how to relate people in a family tree in a relational database).

1 answer

1


The link posted in the comment is quite complete, however, I will put here an example of table creation.

The simplest way would be for each record to point to its father (for theoretically, every father is also a son), this approach makes it possible for each father to have N children, and each Son can only have one Father (which in the case of the family tree is not the case, because each child has two "parents"), thus, as we have a known and maximum number 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 downside of this approach is that Sqlite does not support a recursive Query, that is, fetching a record in the person table and making a "Join" with another record in the same table, so you will not be able to have the Father information in the same query as the child (unless you do a subquery, which is a little more complex for those who are starting out), so in practice, you would have to find the child, and then query with the id_pai that you found on the first record.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.