Question

I've built an iOS application around an sqlite database. One of the tables assigns a drill down tables values, order they are to be displayed in the table, and then the menus that follow. A basic tree I suppose.

The way i've built it, the only way that I could think of (this being the first time I've ever touched sqlite).. it's just a real pain in the rear because it's virtually impossible to modify.

If I have to restructure the menus and add or remove one of the menus then everything that follows after the modification has to have it's parent adjusted to compensate.

Somebody hit me over the head with whatever I'm not thinking of because I'm sure it's obvious.

Representation of structure:

id   menu          icon          level   parent  
===========================================================  
1    colors        color.jpg     1       NULL
-----------------------------------------------------------
2    shapes        shapes.jpg    1       NULL
-----------------------------------------------------------
3    red           red.jpg       2       1 
-----------------------------------------------------------
4    green         green.jpg     2       1  
-----------------------------------------------------------
5    square        square.jpg    2       2  
-----------------------------------------------------------
6    roses         roses.jpg     3       3
-----------------------------------------------------------
7    grass         grass.jpg     3       4

So if I decide to add a new menu at level 1, I have to either live with a horribly messy database or modify all the parent values and id's for every entry after the insertion.

Was it helpful?

Solution

What you want to do is not easy with a table (see What is the best way to store a tree structure in a relational DB?).

It might be better to store the menu structure in a single, recursively-organized JSON or XML object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top