Creating a database using c programming

I want to create a database using C programming.

I want to create an employee database system and want to dynamically update it. please call me how can I go further.

I need to do this for an embedded system that is like flash memory. I need to save the database on this flash, and I will need to dynamically update it. Document and suggestions are valuable.

+6
source share
3 answers

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

Code example:

 /* 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 information on creating a database in c, you can get a guide from the next video

+5
source

Do you have an OS? linux, qnx? If so, check if there are any solutions available. For instance. mysql, postgresql, sqlite. If there is anything there, check their documents.

If you're on bare metal, keep reading.

I think the best way to get started is to use a simple hash table so that you have a fast query time.

The U-boot hash table may be a good start.

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

Then you need to save this in flash. I would keep at least two copies of your data to protect against power outages during recording. To do this, you need some checksumming to add to your data structure, for example. 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 would like to compress the data in some way. I really recommend using the heatshrink compression algorithm. It is simple and works even on atmel avrs

+1
source

You can create a database in c, but it is very inefficient and it requires a lot of effort, so it would be better to use mysql or postgres to process the database.

0
source

Source: https://habr.com/ru/post/947278/


All Articles