How to Create Database in C Language

Leave a Comment



Have you ever thought of Creating DATABASE in C LANGUAGE ?
Or you might be asked in school or college to create temporary database in C Language using File Handling !

So, i thought of this and have created temporary database of taking Employee Details in C language using File Handling.

Like you even i was searching on Google for the same but hasn't found anything. So, I thought let's write our own piece of code and let's share it with the people :) 

Look at the Snap Shot below :-



I wrote about 600-650 lines of code to create database in C Language . The Program is capable of


  1. Building Employee Table
  2. List the Employee Tables 
  3. Insert Query
  4. Delete Query
  5. Edit Query
  6. Search Record
  7. Sort the table

================================== CODE ===============================




#include<string.h>
#include<dos.h>
#include<conio.h>
#include<stdio.h>
#define max 20
struct employee
{
char name[20];
long int code;
char designation[20];
long int exp;
long int age;
};
struct employee emp[max],tempemp[max],sortemp[max],sortemp1[max];
int num=0;
FILE *fp;
void main()
{
void build();
void list();
void insert();
void deletes();
void edit();
void search();
void sort();
void menu();
       // void box(int,int,int,int);
char option;
clrscr();
menu();
     // box(18,4,65,6);
while((option=getchar())!='q')
{
switch(option)
{
case 'b':
build();
break;
case 'l':
list();
break;
case 'i':
insert();
break;
case 'd':
deletes();
break;
case 'e':
edit();
break;
case 's':
search();
break;
case 'n':
sort();
break;
}

menu();
}
}
void menu()
{
clrscr();
highvideo();
printf(" ");
printf("\n\t\t*****WelCome To Employee Data Centre*****");
// normvideo();
printf("\n");
printf(" ");
printf("\nPress b---->Built The Employee Table ");
printf(" ");
printf("\nPress l---->List The Employee Table");
printf(" ");
printf("\nPress i---->Insert New Entry");
printf(" ");
printf("\nPress d---->Delete An Entry");
printf(" ");
printf("\nPress e---->Edit An Entry");
printf(" ");
printf("\nPress s---->Search Arecord");
printf(" ");
printf("\nPress n---->Sort The Table");
printf(" ");
printf("\nPress q---------->QUIT ");
printf(" ");
printf("\nOption Please ----->");
}
void build()
{
int i=0;
clrscr();
highvideo();
printf("Build The Table ");
printf("\n");
// normvideo(); // to proper format text it will include in <conio.h> 
printf("\nmaximum number of entries ----- > 20");
printf("\n");

printf("\nhow many do you want ----->");
scanf("%d",&num);
clrscr();
fp=fopen("D:\\input.txt","wb");
printf("\nEnter The Following Items");
for(i=0;i<=num-1;i++)
{
printf(" \nName ");
fflush(stdin);
scanf("%s",&emp[i].name);
fprintf(fp,"%s",emp[i].name);
printf("\nCode ");
scanf("%ld",&emp[i].code);
fprintf(fp,"%ld",emp[i].code);
printf("\nDesignation ");
fflush(stdin);
gets(emp[i].designation);
fprintf(fp,"%s",emp[i].designation);
printf("\nYears of Experience ");
scanf("%ld",&emp[i].exp);
fprintf(fp,"%ld",emp[i].exp);
printf("\nAge ");
scanf("%ld",&emp[i].age);
fprintf(fp,"%ld",emp[i].age);
}
fclose(fp);
printf("\ngoing to main menu");
delay(500);
}
void list()
{
int i=0;
clrscr();
highvideo();
printf(" ********List The Table********");
printf("\n");
highvideo();
fp=fopen("d:\\input.txt","rb");
printf(" Name\tCode\tDesignation\t Years(EXP)\t  Age");
for(i=0;i<=num-1;i++)
{
// normvideo();
  // fscanf(fp,"%s",&emp[i].name);
printf("\n%s",emp[i].name);
  // fscanf(fp,"%ld",&emp[i].code);
printf("\t%ld",emp[i].code);
  // fscanf(fp,"%s",&emp[i].designation);
printf("\t\t%s",emp[i].designation);
  // fscanf(fp,"%ld",&emp[i].exp);
printf("\t\t%ld",emp[i].exp);
  // fscanf(fp,"%ld",&emp[i].age);
printf("\t%ld",emp[i].age);
printf("\n");
}
printf("\ngoing to main menu");
getch();
}
void insert()
{
int i=num;
clrscr();
num+=1;
highvideo();
printf("\n**********Insert New Record*************");
printf("\n");
// normvideo();
printf("\n\nEnter The Following Items");
fp=fopen("D:\\input.txt","wb");
printf("\n\nName :- ");
fflush(stdin);
gets(emp[i].name);
fprintf(fp,"%s",emp[i].name);
printf("\nCode :- ");
scanf("%ld",&emp[i].code);
fprintf(fp,"%ld",emp[i].code);
printf("\nDesignation :- ");
fflush(stdin);
gets(emp[i].designation);
fprintf(fp,"%s",emp[i].designation);
printf("\nYears of Experience :- ");
scanf("%ld",&emp[i].exp);
fprintf(fp,"%ld",emp[i].exp);
printf("\nAge :- ");
scanf("%ld",&emp[i].age);
fprintf(fp,"%ld",emp[i].age);
printf("\n\n");
printf("\nRecord inserted going to main menu");
delay(500);

}
void deletes()
{
int code;
int check;
int i;
clrscr();
highvideo();
printf("\n*************Delete An Entry***************");
// normvideo();
printf("\n");
printf("\n\nEnter An JobCode To Delete That Entry :- ");
scanf("%ld",&code);
for(i=0;i<=num-1;i++)
{
if(emp[i].code==code)
{
check=i;
}
}
for(i=0;i<=num-1;i++)
{
if(i==check)
{
printf("Record Deleted......");
continue;
}
else
{
if(i>check)
{
tempemp[i-1]=emp[i];
printf("Record Deleted......");
}
else
                    {
tempemp[i]=emp[i];
}
}
}
num--;
for(i=0;i<=num-1;i++)
{
emp[i]=tempemp[i];
}
}
void edit()
{
int i;
int jobcode;
void editmenu();
void editname(int);
void editcode(int);
void editdes(int);
void editexp(int);
void editage(int);
char option;
clrscr();
highvideo();
printf("\n **********Edit An Entry************* ");
printf("\n\n");
// normvideo();
printf("\nEnter An jobcode To Edit An Entry----> ");
scanf("%ld",&jobcode);
editmenu();
for(i=0;i<=num-1;i++)
{
if(emp[i].code==jobcode)
{
while((option=getchar())!='q')
{
switch(option)
{
case 'n':
editname(i);
break;
case 'c':
editcode(i);
break;
case 'd':
editdes(i);
break;
case 'e':
editexp(i);
break;
case 'a':
editage(i);
break;
}
printf("Record Edited........");
editmenu();
}
}
}
}
void editmenu()
{
clrscr();
printf(" \nWhat Do You Want To edit");
printf("\n n--------->Name");
printf("\n c--------->Code");
printf("\n d--------->Designation");
printf("\n e--------->Experience");
printf("\n a--------->Age");
printf("\n q----->QUIT");
printf("\n Options Please ---->>> ");
}
void editname(int i)
{
printf("\nEnter New Name-----> ");
fflush(stdin);
gets(emp[i].name);
}
void editcode(int i)
{
printf("\nEnter New Job Code-----> ");
scanf("%ld",&emp[i].code);
}
void editdes(int i)
{
printf("\nEnter new designation-----> ");
fflush(stdin);
gets(emp[i].designation);
}
void editexp(int i)
{
printf("\nEnter new Years of Experience");
scanf("%ld",&emp[i].exp);
}
void editage(int i)
{
printf("\nEnter new Age");
scanf("%ld",&emp[i].age);
}
void search()
{
int jobcode,i;
clrscr();
highvideo();
printf("\n\t\t\tWelcome To Search Of Employee Database");
// normvideo();
printf("\n\n");
printf("\n\nYou Can Search Only By Jobcode Of An Employee");
printf("\nEnter Code Of An Employee");
scanf("%ld",&jobcode);
for(i=0;i<=num-1;i++)
{
if(emp[i].code==jobcode)
{
printf("\n Name Code Designation Years(EXP) Age");
printf("\n ------------------------------------------------------");
printf("\n Employee Name :- %s",emp[i].name);
printf("\nEmployee Code :- %ld",emp[i].code);
printf("\nEmployee Designation :- %s",emp[i].designation);
printf("\nEmployee Exp :- %ld",emp[i].exp);
printf("\nEmployee age :- %ld",emp[i].age);
printf("\n");
}
}
printf("\ngoing to main menu");
getch();
}
void sort()
{
void sortmenu();
void sortname();
void sortcode();
void sortdes();
void sortexp();
char option;
void sortage();
clrscr();
highvideo();
printf("\nSort The Databse By JobCode");
// normvideo();
printf("\n\n");
sortmenu();
while((option=getchar())!='q')
{
switch(option)
{
           case 'n':
sortname();
break;
case 'c':
sortcode();
break;
case 'd':
sortdes();
break;
case 'e':
sortexp();
break;
case 'a':
sortage();
break;
}
sortmenu();
}
}
void sortmenu()
{
clrscr();
printf("\n What Do You Want To edit");
printf("\n n--------->Name");
printf("\n c--------->Code");
printf("\n d--------->Designation");
printf("\n e--------->Experience");
printf("\n a--------->Age");
printf("\n q----->QUIT");
printf("\n Options Please ---->>> ");
}



void sortname()
{
int i,j;
struct employee temp[max];
clrscr();
for(i=0;i<=num-1;i++)
{
sortemp1[i]=emp[i];
}
for(i=0;i<=num-1;i++)
{
for(j=0;j<=num-1;j++)
{
if(strcmp(sortemp1[i].name,sortemp1[j].name)<=0)
{
temp[i]=sortemp1[i];
sortemp1[i]=sortemp1[j];
sortemp1[j]=temp[i];
}
}
}
for( i=0;i<=num-1;i++)
{
printf("\n Name Code Designation Years(EXP) Age");
printf("\n ------------------------------------------------------");
for( i=0;i<=num-1;i++)
{
printf("\nEmployee Name :-%s",sortemp1[i].name);
printf("\nEmployee code :-%ld",sortemp1[i].code);
printf("\nEmployee Designation :-%s",sortemp1[i].designation);
printf("\nEmployee exp :-%ld",sortemp1[i].exp);
printf("\nEmployee age :-%ld",sortemp1[i].age);
printf("\n");
}
printf("\nPress Any Key To Go Back");
getch();
}
 }
void sortcode()
{
int i,j;
struct employee temp[max];
clrscr();
for(i=0;i<=num-1;i++)
{
sortemp1[i]=emp[i];
}
for(i=0;i<=num-1;i++)
{
for(j=0;j<=num-1;j++)
{
if(sortemp1[i].code<sortemp1[j].code)
{
temp[i]=sortemp1[i];
sortemp1[i]=sortemp1[j];
sortemp1[j]=temp[i];
}
}
}
for( i=0;i<=num-1;i++)
{
printf("\n Name   Code   Designation   Years(EXP)   Age");
printf("\n ------------------------------------------------------");
for( i=0;i<=num-1;i++)
{
printf("\n%s",sortemp1[i].name);
printf("\n%ld",sortemp1[i].code);
printf("\n%s",sortemp1[i].designation);
printf("\n%ld",sortemp1[i].exp);
printf("\n%ld",sortemp1[i].age);
printf("\n");
}
printf("\nPress Any Key To Go Back");
getch();
} }
void sortdes()
{
int i,j;
struct employee temp[max];
clrscr();
for(i=0;i<=num-1;i++)
{
sortemp1[i]=emp[i];
}
for(i=0;i<=num-1;i++)
{
for(j=0;j<=num-1;j++)
{
if(strcmp(sortemp1[i].designation,sortemp1[j].designation)<=0)
{
temp[i]=sortemp1[i];
sortemp1[i]=sortemp1[j];
sortemp1[j]=temp[i];
}
}
}
for( i=0;i<=num-1;i++)
{
printf("\n Name Code Designation Years(EXP) Age");
printf("\n ------------------------------------------------------");
for( i=0;i<=num-1;i++)
{
printf("\n%s",sortemp1[i].name);
printf("\n%ld",sortemp1[i].code);
printf("\n%s",sortemp1[i].designation);
printf("\n%ld",sortemp1[i].exp);
printf("\n%ld",sortemp1[i].age);
printf("\n");
}
printf("\nPress Any Key To Go Back");
getch();
}
 }
void sortage()
{
int i,j;
struct employee temp[max];
clrscr();
for(i=0;i<=num-1;i++)
{
sortemp1[i]=emp[i];
}
for(i=0;i<=num-1;i++)
{
for(j=0;j<=num-1;j++)
{
if(sortemp1[i].age<sortemp1[j].age)
{
temp[i]=sortemp1[i];
sortemp1[i]=sortemp1[j];
sortemp1[j]=temp[i];
}
}
}

for( i=0;i<=num-1;i++)
{
printf("\n Name Code Designation Years(EXP) Age");
printf("\n ------------------------------------------------------");
for( i=0;i<=num-1;i++)
{
printf("\n%s",sortemp1[i].name);
printf("\n%ld",sortemp1[i].code);
printf("\n%s",sortemp1[i].designation);
printf("\n%ld",sortemp1[i].exp);
printf("\n%ld",sortemp1[i].age);
printf("\n");
}
printf("\nPress Any Key To Go Back");
getch();
} }
void sortexp()
{
int i,j;
struct employee temp[max];
clrscr();
for(i=0;i<=num-1;i++)
{
sortemp1[i]=emp[i];
}
for(i=0;i<=num-1;i++)
{
for(j=0;j<=num-1;j++)
{
if(sortemp1[i].exp<sortemp1[j].exp)
{
temp[i]=sortemp1[i];
sortemp1[i]=sortemp1[j];
sortemp1[j]=temp[i];
}
}
       }
     for( i=0;i<=num-1;i++)
{
printf("\n Name Code Designation Years(EXP) Age");
printf("\n ------------------------------------------------------");
for( i=0;i<=num-1;i++)
{
printf("\n%s",sortemp1[i].name);
printf("\n%ld",sortemp1[i].code);
printf("\n%s",sortemp1[i].designation);
printf("\n%ld",sortemp1[i].exp);
printf("\n%ld",sortemp1[i].age);
printf("\n");
}
printf("\nPress Any Key To Go Back");
getch();

}}




Now, 

STEP 1:- Just copy the above code and paste it in the "Notepad".

STEP 2:- Go to "File" or simple press alt + f and Press "Save as" and save it with " .c " extension for example :- "DatabaseInC.c" or "DatabaseInC.cpp" .

STEP 3:- Now, Save it to the desired Location. I would Suggest save it to "BIN" Folder Most Probably it would be here " C:\TC\BIN " .

STEP 4:- Open "Turbo C" or "Borland C" and Press "File" Option or simply Press " alt + f " then, Click open or Simply Press " f3 "and write the Path wherever you have saved the file.

If you've followed all the Instruction mentioned above . The File Would be here " C:\TC\BIN " and search for the file " DatabaseInC.c " or " DatabaseInC.cpp " and hit enter.



Step 5 :- Run the Program by pressing  " alt + f9 " :)
Enjoy You've Just Created the Database In C Language. 


Also See :

How to Hack Wifi Password in 2 Minutes

How to Get Facebook Auto Likes and Comments on Facebook Status, Pics and Fan Page

How to Prevent Blogger Blog from Redirection to Country Specific URL

How to Hide Last Seen on Whatsapp in Iphone and Android Phone

0 comments:

Post a Comment