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.
Creating a database application in C/C++ is a daunting task, especially for a novice programmer. Although the actually code is quite simple, it is the configuration issues such as importing right library, drivers to use, how to access them, and so forth, that make it an uphill battle.
Jasmine/C is a C-based database proqramninq language that allows the handling of persistent objects in Jasmine databases. The language is used to write methods for objects and application programs. Both navigational and associative access to objects are supported.
Both are programming languages (both Turing complete depending on your exact SQL dialect), however... C (C++, C#, Java, Visual Basic) are Procedural programming languages. You specify a sequence of steps for the computer to take.
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 .
/* 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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With