Thread: How To Create Database Tables in C

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    33

    How To Create Database Tables in C

    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:

    Code:
    struct table{
        union{
            int number;
            char *text;
        }column1;
        
        union{
            int number;
            char *text;
        }column2;
    
        union{
            int number;
            char *text;
        }column3;
    
        ....
    
    };
    Is there a better way to do this?

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Code:
    struct table{
        union{
            int number;
            char *text;
        }column1;
        
        union{
            int number;
            char *text;
        }column2;
    
        union{
            int number;
            char *text;
        }column3;
    
        ....
    
    };
    Why not use an array? You could also use dynamic memory allocation to get an array the right size for however much data you need to work with.

    Using structs would help manage the data types as well, eg:
    Code:
    struct entry {
        union{
            int number;
            char *text;
        } data;
    
        int datatype;
    };
    so that you can keep track of what kind of data you're working with.
    Consider this post signed

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    33
    This makes things better indeed. Thanks

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    typedef struct t_dbData
      { void *Data;
         size_t Size; 
         int   Type; }
      dbData, *pdbData;
    
    // make a table
    static dbdata [10][10];
    Now you have an array of structs that can store whatever you hand them...
    use malloc() and memcpy() to store data, use the Size field to know how much memory is allocated, assign each type a numerical value (possibly in an enum) and use the Type field to tell you what kind of data is stored there.

    It can store strings, ints, arrays of ints, floats, arrays of floats, other dbdata arrays, whatever you like as much as you like.

    From there you simply have the monumental task of writing code to manipulate the cells and data in your dbdata array.
    Last edited by CommonTater; 10-08-2011 at 08:00 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create connection to database in C
    By gmalachi in forum C Programming
    Replies: 2
    Last Post: 12-29-2010, 05:33 AM
  2. create and populate create bidimensional array
    By darkducke in forum C Programming
    Replies: 0
    Last Post: 12-03-2010, 07:06 AM
  3. Database to Database Transfer?
    By draggy in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2007, 10:50 AM
  4. Database size, Tables , Server, BCB6
    By Leite33 in forum Windows Programming
    Replies: 0
    Last Post: 11-06-2006, 12:17 AM
  5. Replies: 1
    Last Post: 10-09-2001, 10:20 PM

Tags for this Thread