I need to code a small/simple database application using C.
My idea is to use a B-Tree to store the items of each table. The problem I am facing is that tables need to be flexible to hold an unknown number of columns, and each column can be either a STRING or an INT. For example, with this command:
CREATE TABLE student (STRING name, INT age)
I would need to create a table that holds a string and an integer. With this command instead:
CREATE TABLE grade (INT grade1, INT grade2, INT grade3)
I would need to create a table that holds three integers.
How can achieve such flexibility?
My only idea so far is to create a struct with several unions inside it, where each union can be either a STRING or an INT. I would also need to put a lot of unions inside, to be sure to accommodate all the columns requested by the table. For example:
Is there a better way to do this?Code:struct table{ union{ int number; char *text; }column1; union{ int number; char *text; }column2; union{ int number; char *text; }column3; .... };