im given an very tuff assignment like this
a. Introduction
we have read all input from q sql file.the name of sql file should be read by a command line.please any body help me to do? i tried some.
support of SQL of your ADT table to CREATE TABLE,
DROP TABLE and DELETE statements. Assume that your program can support only one table at
any time.
To enable efficient processing of DELETE statements, use pointer-based linked list as your
primary storage of data in the table. This avoids shifting of other tuples when a set of tuples is
deleted.
b. CREATE TABLE Statement
The CREATE TABLE statement should support the creation of one relational table of any
table name, and with a schema of up to six attributes of any data type of either STRING( n ) or
NUMBER. STRING (n) means a character string with a maximum of n characters, where n is a
value between 1 to 40. NUMBER means a number from -999999.99 to 999999.99.
Hint: Use data dictionary to record attribute names and attribute data types and refer to it when
processing SQL statements. This provides a higher abstract level of processing from native data
types provided by C++.
An Example of a CREATE TABLE statement is shown below

CREATE TABLE staff
(
sno STRING( 5 ) PRIMARY KEY,
name STRING( 40 ),
salary NUMBER,
position STRING ( 10 ),
telephone STRING ( 15 ),
gender STRING ( 1 )
);

Assume that the first attribute is always the primary key and must be unique and not contain a
NULL value.
Your program should display
TABLE staff created.
after processing the statement.
c. DROP TABLE Statement
The DROP TABLE statement should drop a table from data dictionary and clean up
memory used the table.
An Example of a DROP TABLE statement is shown below.
DROP TABLE staff;
Your program should display
TABLE staff dropped.
after processing the statement.
d. DELETE Statement
The DELETE statement to support consists of two lines as shown below.
DELETE FROM staff
WHERE sno = “A1234”;
The features to support in WHERE clause is similar to the WHERE clause of a SELECT statement.
There is always only one condition which uses the = operator.
Your program should display the number of tuples deleted, such as,
5 tuples deleted.
after processing the statement.


my codings.

#include <iostream>
      #include <string>
      #include <ctype.h>
      #include<iomanip>

      using namespace std;
      /*----------------------------------------------------------------------------*/
      /* Define Functions*/

      void addNewstaff(void);
      void listAll(void);
      void deletestaff(void);
      int findnum (int);

      /*----------------------------------------------------------------------------*/
      /* Define Structures*/

      typedef struct staff{
      int number; /*unique account number*/
      char sno[5];
      char gender[8];/*contains gender*/
      char name[20]; /*contains name*/
      char phone[15]; /*contains phone number*/
      char position[30];/*contains position*/
      long salary;
      struct staff *next; /*next is used to navigate through structures.*/
      int count; /*count is used to input comments into array*/
      } staff;
     staff *firstnode,*currentnode,*newnode; /*pointers*/
      /* firstc is used to point to first record in list
      currentc points to current record in list
      newc contains address of new structure/node
      */
      int cnum = 0; /*gives unique account numbers*/


      /*----------------------------------------------------------------------------*/
      /* Main Function */

      int main()
      {
      FILE *datafile;
      char *filename = "staffdatabase.txt";/*declare file name*/
      char ch;
      firstnode = NULL;

      datafile = fopen(filename,"r");/* open file for reading*/

      if(datafile)
      {
      //firstc = (struct staff *)malloc(sizeof(struct staff));
      /*use of malloc to set aside memory relative to size of structure contact*/
      currentnode = firstnode; /*make first record current*/
      while(1) /*endless while loop. a NULL pointer in final node ends loop*/
      {
     // newc = (struct staff *)malloc(sizeof(struct staff));
      fread(currentnode,sizeof(struct staff),1,datafile);
      if(currentnode->next == NULL) /* NULL indicates end of node list*/
      break;
      currentnode->next = newnode; /* pointer referencing next node*/
      currentnode->count=0; /* initiates count for comments*/
      currentnode = newnode; /* make current record new*/
      }
      fclose(datafile); /* close file - good practice to cloe files after use*/
      cnum = currentnode->number;

      }

      do
      {
      fflush(stdin);
      cout<<"\nWelcome To The staff Database"<<endl;/* print menu messages*/
      cout<<"-- -----------------------------"<<endl;
      cout<<"1 - Add a new staff"<<endl;
      cout<<"2 - Delete staff"<<endl;
      cout<<"3 - List all staffs"<<endl;

      cout<<"-- -----------------------------"<<endl;
      cout<<"Q - Save and quit\n";
    cout<<"\tYour choice:";
      ch = getchar();
      ch = toupper(ch);/*changes user input case to upper case*/
      switch(ch) /*stores in ch variable.*/
      {
      case '1':
      cout<<"Add a new staff\n";
      fflush(stdin);
      addNewstaff();//call addNewcontact function
      break;
      case '2':
      cout<<"Delete a staff\n";
      deletestaff();
      break;
      case '3':
      cout<<"List all staffs\n";
      listAll();
      break;

      case 'Q':
      cout<<"Save and quit\n";
      default:
      break;
      }
      }
      while(ch != 'Q');

      /*
       * Save the records to disk
       */
      currentnode = firstnode;

      if(currentnode == NULL)
      return(0); /*no data to write*/

      datafile = fopen(filename,"w"); /*open file to write*/

      if(datafile == NULL)
      {
      cout<<"Error writing to " <<filename;
      return(1);
      }
      /* Write each record to disk*/
      while(currentnode != NULL)
      {
      fwrite(currentnode,sizeof(struct staff),1,datafile);
      currentnode = currentnode->next;
      }
      fclose(datafile); /*closes data file*/
      return(0);
      }
      /*----------------------------------------------------------------------------*/
      void addNewstaff(void) /* add new contact function*/
      {
      newnode = (struct staff *)malloc(sizeof(struct staff));
      /*allocates memory for new structure.*/

      /*
       * Checks to see whether this is the first record in file
       * If so, then all pointers are initialized to this record,
       */

      if(firstnode==NULL)
      firstnode = currentnode = newnode;

      /*
       * if not, end of structure list is obtained
       */

      else
      {
      currentnode = firstnode; /* make the first record the current one*/


      while(currentnode->next != NULL)currentnode = currentnode->next;
      /* and loop through all records*/
      currentnode->next = newnode; /* pointer to next node */
      currentnode = newnode; /* make current record the new one*/
      }

      /* update the structure */

      cnum++;
      cout<<"staff number" <<cnum<<"  ";
      currentnode->number = cnum; /*cnum is used to give unique account numbers*/

      cout<<"Enter staff name : ";
      cin>>currentnode->name;

      cout<<"Enter staff Phone number : ";
      cin>>currentnode->phone;

      cout<<"Enter staff gender : ";
      cin>>currentnode->gender;

      cout<<"Enter staff position : ";
      cin>>currentnode->position;

      cout<<"Enter staff salary  : ";
      cin>>currentnode->salary;

      cout<<"staff added!";
      currentnode->count=0;

      /*
       * gives the new record a NULL pointer
       * to show it's the last record:
       */

      currentnode->next = NULL;
      }
      /*----------------------------------------------------------------------------*/
      void listAll(void) /* list all contacts function*/
      {
      if(firstnode==NULL)

      cout<<"There are no staffs to display!"; /*prints message*/

      else
      {
      cout<<"sno"<< setw(10)<<"Name"<<setw(15)<<"gender"<<setw(15)<<"Phone"<<setw(15)<<"salary"<<setw(15)<<"position"<<endl;
      cout<<"-------------------------------------------------------------------------------"<<endl;
      /*prints table titles*/
      currentnode=firstnode;

      do
      {

      cout<<
      currentnode->number<< setw(10)<<
      //currentc->sno<<setw(10)<<
      currentnode->name<< setw(15) <<
      currentnode->gender<<setw(15)<<
      currentnode->phone<<setw(15)<<
      currentnode->salary<< setw(15)<<
      currentnode->position;
      cout<<endl;
      /*prints values of number, name, gender,phone,salary and position*/
      }

      while((currentnode=currentnode->next) != NULL);
      }
      }
      /*----------------------------------------------------------------------------*/
      void deletestaff(void) /*delete contact function */
      {
      int record;
      struct staff *previousa;

      if(firstnode==NULL)
      {
      cout<<"There are no staffs to delete!";
      return;
      }

      listAll(); /* show all records*/
      cout<<"Enter staff account number to delete: ";
      cin>>record;

      currentnode = firstnode;

      while(currentnode != NULL)
      {
      if(currentnode->number == record)
      {
      if(currentnode == firstnode) /*if record to be deleted is the first record*/
      firstnode=currentnode->next; /*reset firstc to point at next record as first*/
      else
      previousa->next = currentnode->next;/*previous pointer used if record*/
      /*to delete is not the first*/
      free(currentnode); /*frees memory <deletes>*/
      cout<<"staff "<<record <<" deleted!\n";
      return;
      }

      else
      {
      previousa = currentnode;
      currentnode = currentnode->next;
      }
      }
    cout<<"staff "<<record<<" not found!\n";
      }
      /*----------------------------------------------------------------------------*/


      int findnum (int recordnum)
      {
      int record;
      record = recordnum;
      currentnode = firstnode;
      while(currentnode != NULL)
      {

      if(currentnode->number == record)
      {
      return 1;
      }

      else
      {
      currentnode = currentnode->next;
      }
      }
      return -1;
      }

i did it in a normal way .please any body guide me 2 do in tht way i mentioned before.

Recommended Answers

All 7 Replies

The requirement for CREATE TABLE indicates that the table contents should be read from a text file (aka schema), not hard-coded in your program.

One common way to do that is to create a structure that contains the information for each field in the table

struct field
{
    std::string field_name;
    int length; // only for strings
    int dataType; // either 1 = string, or 2 = numeric
    bool PrimaryKey; // true = this is a primary key field
};

Now when the program reads the schema file it should build a linked list or c++ vector of the above structures for each field.

What is ambiguous to me is: does your instructor expect you to actually create a table in a real SQL database such as MS-Access ? Or are you to just use a text file of your own design for it?

i should use sql file as database format.please help me to do this

>>i should use sql file as database format
That means nothing because there are thousands of sql file formats.

>>please help me to do this
I already have told you how I would approach the problem. But since I have not been in your class lectures I can't be sure if I was right or wrong.

The first thing I would do is write a program that reads the schema file and builds the linked list I already mentioned.

thank you fot your help.i hope your approach will be right.im waiting for your reply

>>im waiting for your reply
I'm not going to write it for you. YOU write the code and we will halp with whatever questions you may have. But the code you turn in must be your own, not ours or mine.

commented: Well said :) +3

ok how to read a fiel by using command line?

How to read input file by command line in c++?
im given a task to read the input file from .sql file by using command line .
int main(int argc,char* argv[])
i know this step only.how to give our input file name when we using this ?pls anybody help me to understand?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.