7

I want to create database using C programming.

I want to create the employee database system and want to update it dynamically. please guide me how can I go ahead.

I have to do it for embedded system which as flash memory. the database is need to be stored on that flash and I need to be able to update it dynamically. Document and suggestions are valuable.

3
  • 6
    try Sqlite Commented Jun 14, 2013 at 11:28
  • If you want to make a MySQL database using C programming, you can make use of MySQL C Connector. Commented Jun 14, 2013 at 11:37
  • 1
    I don't see what this has to do with the Linux kernel.
    – idoby
    Commented Jun 16, 2013 at 21:06

2 Answers 2

11

You can use structs and file operations to write and read from the file . However the operations may not be too fast and efficient as in case of MYSQL or any other database .

Example code :

/*  employee database program       */

#include <stdio.h>
#include <string.h>

typedef struct vehicle
{
    char name[100];
    int roll;
    int salary;
    char address[100];
    int join_year;
}record;

int main(void)
{
    int i , choice;
    FILE *fp1,*fp2;
    char oname[100];
    record det;
    int recsize;
    char c;

    fp1 = fopen("record.dat" , "r+");
    if(fp1 == NULL)
    {
        fp1 = fopen("record.dat" , "w+");
        if(fp1 == NULL)
        {
            printf("error in opening file : \n");
            return -1;
        }
    }
    recsize = sizeof(det);

    fseek(fp1 , 0 ,SEEK_END);
    printf("Enter employee Name : ");
    scanf("%[^\n]" , det.name);
    printf("Enter roll number   : ");
    scanf("%d" , &det.roll);
    printf("Enter the salary    : ");
    scanf("%d" , &det.salary);
    scanf("%c" , &c);
    printf("Enter address   : ");
    scanf("%[^\n]" , det.address);
    printf("Enter joining year  : ");
    scanf("%d" , &det.join_year);
    fwrite(&det,recsize,1,fp1);
}

For more details about making a database in c you can take guidance from the following video

4
  • ill try this and will come up if got doubt
    – amar
    Commented Jun 14, 2013 at 7:51
  • How can I store C structures in human-readable files.the above code results in binary format so i want to make it human readable how can i do it.
    – amar
    Commented Jun 27, 2013 at 9:31
  • @amar you could use JSON to do it. Then use a function taking the struct as an parameter to convert it to a JSON string. However this is somewhat tedious in C...
    – borchero
    Commented Sep 20, 2015 at 22:15
  • @borchero This would be great if you want it to be human readable, however you'll be adding a lot of overhead. Commented Nov 9, 2021 at 13:47
3

Do you have an OS there? linux, qnx? If you do, check if there are any native solutions available. E.g. mysql, postgresql, sqlite. If there's anything there - check out their docs.

If you are on bare metal, read on.

I think the best way to start is using a simple hash table, so that you'll have fast query times.

U-boot's hashtable may serve you as a good start.

https://github.com/lentinj/u-boot/blob/master/lib/hashtable.c

Next, you'll need to store that in flash. I'd keep at least two copies of your data for the sake of protection from power outage during write. To achieve that you'll need some checksumming to add to your data structure, e.g. crc32. See this:

http://www.barrgroup.com/Embedded-Systems/How-To/CRC-Calculation-C-Code

Finally, if you have a lot of data and (not so much) flash you'd want to compress the data somehow. I really recommend using heatshrink compression algorithm. It's simple and works even on atmel avrs

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.