Presentation is loading. Please wait.

Presentation is loading. Please wait.

OUTLINE We will design a sample “Library” database in C language.

Similar presentations


Presentation on theme: "OUTLINE We will design a sample “Library” database in C language."— Presentation transcript:

1 OUTLINE We will design a sample “Library” database in C language.
For permanent recording, we will use FILING. For memory recording, we will use arrays.

2 Introduction to Databases
Database is a kind of Software which can perform different operations (functions) on a large group of records (Transactions). Example of operations include:- Add transaction, delete transaction, update (modify) transactions, search transactions. Where Databases are used:- Database software are frequently used in Banks, Hospitals, Post offices, Weather Departments, Government offices, Libraries, Universities etc. Why we need Databases:- For future purposes. Remember: Past is the best predict of future.

3 Our Library Database System
Will perform the following operations:- User can add a book in the Database. User can delete a book in the Database. User can modify a book in the Database. User can list all the books in the Database. User can search any book in the Database.

4 Press key to select options:-
Press ‘1’ to add new book. Press ‘2’ to delete a book. Press ‘3’ to modify a book. Press ‘4’ to list all the books. Press ‘5’ to search the book. Press ‘Esc’ to exit the system. User key = 1 USER has selected add new book option. Enter the ISBN:- Enter the name:- Enter the author name:- --

5 Library Database system Flow chart
start Read database from FILE and store in array (memory) Add new book Delete book Get key Modify book key List books Search book Store database from array to FILE Exit

6 Selection of Data structures
ISBN Book Name Author Name Publisher Number of Copies Year of Publish 1293 Network Security Martin Waley 4 1998 9382 Data mining Muhammad Zaki Wrox 6 2003 9993 Data warehousing Stephen Brobst MIT 8 --- int Copies [ ] int ISBN [ ] char AuthorName [ ][30] int PYear [ ] char Publisher [ ][30] char BookName [ ][30] Our array will be static. We have to specify a certain size at Compile time and it will not change at Execution time (run time).

7 Transactions variables
int ISBN [200]; char BookName [200][30]; char AuthorName [200][30]; char PublisherName [200][30]; int Copies [200]; int PYear [200]; int TransactionCount = 0;

8 Header files and function declaration……
void main (void) { clrscr(); ReadDatabase (); char keyPress; while (1) menu (); cout << “USER key = “; keyPress = getche (); switch (keyPress); case ‘1’: AddBook (); break; case ‘2’: DeleteBook (); break; case ‘3’: ModifyBook (); break; case ‘4’: ListBooks (); break; case ‘5’: SearchBook (); break; case 27: EndSystem (); break; }// end of switch statement } // end of while statement } void menu ( ) { cout << “Press any key to select options:-\n”; cout << “Press ‘1’ to add a new book\n”; cout << “Press ‘2’ to Delete a book\n”; cout << “Press ‘3’ to Modify a book\n”; cout << “Press ‘4’ to List all the books\n”; cout << “Press ‘5’ to Search a book\n”; cout << “Press ‘Esc’ to Exit the system\n”; }

9 Add new Book function (Implementation)
void AddBook ( ) { cout << “USER has selected Add new book option.”; AddBook (TranactionCount); TransactionCount++; } void AddBook (int TransactionID) cout << “\nEnter ISBN: “; cin >> ISBN [TransactionID]; cout << “\nEnter Book Name :”; cin >> BookName [TransactionID]; cout << “\nEnter Author Name:”; cin >> AuthorName [TransactionID]; cout << “\nEnter publisher Name”; cin >> PublisherName [TransactionID]; cout << “\nEnter number of copies:”; cin >> Copies [TransactionID]; cout << “\n Enter publishing year: “; cin >> PYear [TransactionID];

10 Delete Book function (Implementation)
void DeleteBook (void) { int tempISBN = -1; cout << “USER has selected Delete a book option.”; cout << “\nEnter ISBN of Book for deleting: “; cin >> tempISBN; for ( int loop = 0; loop < 200; loop ++ ) if ( tempISBN == ISBN [loop] ) { ISBN [loop] = -1; tempISBN = -2; break; } } if ( tempISBN != -2 ) cout << “\nBook not found.”; else cout << “\nBook deleted successfully”;

11 Modify Book function (Implementation)
void ModifyBook (void) { int tempISBN = -1; cout << “USER has selected Modify a book option.”; cout << “\nEnter ISBN of Book for Modify: “; cin >> tempISBN; for ( int loop = 0; loop < 200; loop ++ ) if ( tempISBN == ISBN [loop] ) tempISBN = -2; break; } if ( tempISBN != -2 ) cout << “\nBook not found.”; else AddBook (loop);

12 List books function (Implementation)
void ListsBook (void) { int tempISBN = -1; cout << “USER has selected List Books option.”; for ( int loop = 0; loop < 200; loop ++ ) if ( ISBN [loop] != -1 ) cout << “\nISBN : “<< ISBN [loop]; cout << “\nBook name : “<< BookName [loop]; cout << “\nAuthor name : “<< AuthorName [loop]; cout << “\nPublisher name : “<< PublisherName [loop]; cout << “\nNumber of copies : “<< Copies [loop]; cout << “\nYear of publish: “<< PYear [loop]; }

13 Search Book function (Implementation)
void SearchBook (void) { char tempBookName[200]; cout << “USER has selected Search a book option.”; cout << “\nBook name to search: “; cin >> tempBookName; for ( int loop = 0; loop < 200; loop ++ ) if ( strcmp (tempBookName, BookName[loop] ) == 0 ) cout << “\nISBN : “<< ISBN [loop]; cout << “\nBook name : “<< BookName [loop]; cout << “\nAuthor name : “<< AuthorName [loop]; cout << “\nPublisher name : “<< PublisherName [loop]; cout << “\nNumber of copies : “<< Copies [loop]; cout << “\nYear of publish: “<< PYear [loop]; }

14 End System function (Implementation)
void EndSystem (void) { FILE *fptr; fptr = fopen (“d:\\LibraryData.txt”, “w”); for ( int loop = 0; loop < 200; loop ++ ) if ( ISBN [loop] != -1 ) // only add valid books fprintf (fptr, “%d %s %s %s %d %d”, ISBN [loop], BookName [loop], AuthorName [loop], PublisherName [loop], Copies [loop], PYear [loop] ); } fclose (fptr); exit (1);

15 Read Database function (Implementation)
void ReadDatabase (void) { FILE *fptr; fptr = fopen (“d:\\LibraryData.txt”, “r”); while (1) { int status = fscanf (fptr, “%d %s %s %s %d %d”, &ISBN [TransactionCount], BookName [TransactionCount], AuthorName [TransactionCount], PublisherName [TransactionCount], &Copies [TransactionCount], &PYear [TransactionCount] ); TransactionCount ++; if ( status == EOF ) break; } fclose (fptr);

16 File and Directory Operations
C provides a number of file/Directory handling operations. Remove file Rename file Make directory Delete directory Search files Execute files

17 Removing file { int flag = remove (“c:\\first.bmp”); if ( flag == 0 )
We can remove any file using int remove (char *filename) function. On success, function returns 0, while on error function returns -1. Include the file “stdio.h”, before using this function. { int flag = remove (“c:\\first.bmp”); if ( flag == 0 ) cout << “File deleted successfully”); else cout << “Can’t remove the file”); }

18 Rename file { int flag = rename (“c:\\first.bmp”, “c:\\second.bmp”);
We can rename any file using int rename (char *oldname, char *newname) function. On success, function returns 0, while on error function returns -1. Include the file “stdio.h”, before using this function. { int flag = rename (“c:\\first.bmp”, “c:\\second.bmp”); if ( flag == 0 ) cout << “File deleted successfully”); else cout << “Can’t remove the file”); }

19 Make/Remove Directory
We can make/remove any directory using int mkdir (char *filename) and int rmdir (char *filename) functions. On success, function returns 0, while on error function returns -1. Include the file “dir.h”, before using this function. { int flag = mkdir (“c:\\mydirectory”); if ( flag == 0 ) cout << “Directory created successfully”); else cout << “Can’t create the directory”); }

20 File searching struct ffblk;
int findfirst(char *search_criteria,struct ffblk *,int argument); int findnext(struct *ffblk); These all functions are declared in “dir.h” file.

21 File searching void main ( void ) { struct ffblk filesearch; int done;
done = findfirst(“c:\\*.*",&filesearch,0); while (done != -1) printf(" %s\n", filesearch.ff_name); done = findnext(&filesearch); }

22 Executing Files We can execute any “exe” file using int system (char *filename) function. On success, function returns 0, while on error function returns -1. Include the file “system.h” and “process.h”, before using this function. { system (“dir”); system (“c:\\first.bmp”); system (“notepad”); }


Download ppt "OUTLINE We will design a sample “Library” database in C language."

Similar presentations


Ads by Google