Skip to content
Permalink
78bd4a05d1
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
323 lines (294 sloc) 11.3 KB
#include<iostream>
#include<mysql.h>
#include<stdio.h>
#include<string>
//#include <sstream>
#include <iomanip>
//database param
#define SERVER "127.0.0.1"
#define USER "root"
#define PASSWORD "123"
#define DATABASE "schedule"
using namespace std; //default namespace
class Schedule{
private:
MYSQL *connect;
MYSQL_RES *res_set;
MYSQL_ROW row;
/*private function to create connection object*/
void getConnection();
/*create all tables if not exist*/
void createTables();
/*public constructor, create connection and make basic tables in not exist*/
public: Schedule();
/*destructur, delete the connection*/
~Schedule(){mysql_close (connect);}
/*return result set of a querry*/
void getResultSet(char* query);
void printSchedule(char* batch_id);
};
void Schedule::getConnection()
{
connect=mysql_init(NULL);
if (!connect)
{
cout<<"MySQL Initialization failed";
}
connect=mysql_real_connect(connect, SERVER, USER, PASSWORD , DATABASE ,0,NULL,0);
if (connect)
{
cout<<"connection Succeeded\n";
}
else
{
cout<<"connection failed\n";
}
}
void Schedule::createTables()
{
getResultSet((char*)"CREATE TABLE IF NOT EXISTS `schedule`.`subject_list`"
" (`sub_id` VARCHAR(10) NOT NULL, `sub_name` VARCHAR(50) NOT NULL, PRIMARY KEY (sub_id) );");
res_set=NULL;
getResultSet((char*)"CREATE TABLE IF NOT EXISTS `schedule`.`batch_list` (`batch_id` VARCHAR(10) NOT NULL, `batch_name` VARCHAR(50) NOT NULL, `room_no` VARCHAR(50) NOT NULL, PRIMARY KEY (batch_id) );");
res_set=NULL;
getResultSet((char*)"CREATE TABLE IF NOT EXISTS `schedule`.`faculty_list` (`fac_id` VARCHAR(10) NOT NULL, `fac_name` VARCHAR(50) NOT NULL, PRIMARY KEY (fac_id) );");
res_set=NULL;
getResultSet((char*)"CREATE TABLE IF NOT EXISTS `schedule`.`routine` (`day` VARCHAR(3) NOT NULL,"
"`time` VARCHAR(20) NOT NULL,"
"`type` VARCHAR(10) NOT NULL,"
"`batch_id` VARCHAR(50) NOT NULL,"
"`fac_id` VARCHAR(50) NOT NULL,"
"`sub_id` VARCHAR(50) NOT NULL,"
"PRIMARY KEY (day,time,type,batch_id,fac_id,sub_id),"
"FOREIGN KEY(batch_id) REFERENCES batch_list(batch_id),"
"FOREIGN KEY(fac_id) REFERENCES faculty_list(fac_id),"
"FOREIGN KEY(sub_id) REFERENCES subject_list(sub_id)"
");");
}
/*public constructor, create connection and make basic tables in not exist*/
Schedule::Schedule()
{
getConnection();
createTables();
}
void Schedule::getResultSet(char* query)
{
//cout<< query;
mysql_query (connect,query);
res_set = mysql_store_result(connect);
}
void Schedule::printSchedule(char* batch_id){
char buffer[1024];
char day[7][10]={"Mon","Tue", "Wed", "Thu", "Fri","Sat","Sun"};
cout<<endl<<"Schedule for:"<<batch_id<<endl;
cout<<"------------------------------------------------------------------------------------------------------------------------------------"<<endl;
cout<<"Day"<<"\t | \t" <<"Class Duration"<<"\t | \t" <<"Type"<<"\t | \t" <<"Faculty Name"<<"\t | \t" <<"Code"<<"\t | \t" <<"Subject Name"<<endl;
cout<<"------------------------------------------------------------------------------------------------------------------------------------"<<endl;
for(int j=0;j<7;j++)
{
sprintf(buffer,"select time,(type),fac_name,(routine.sub_id),sub_name from routine,faculty_list,subject_list where routine.fac_id=faculty_list.fac_id AND routine.sub_id=subject_list.sub_id AND batch_id='%s' AND day=SUBSTR('%s',1,2) order by time;",batch_id,day[j]);
unsigned int i =0;
getResultSet((char*)buffer);
unsigned int numrows = mysql_num_rows(res_set);
cout<<day[j];
while (((row= mysql_fetch_row(res_set)) !=NULL ))
{ //cout<<" %s\n",row[i] !=NULL?row[i] : "NULL";
//cout <<"\t | \t" << row[i] << "\t | \t";
//cout << row[i+1] << "\t |"<< endl;
for(int k=0;k<5;k++)
{
cout <<"\t | \t" << row[k] ;
}
cout<<endl;
i++;
}
//off day
if(i==0)
cout<<endl;
cout<<"------------------------------------------------------------------------------------------------------------------------------------"<<endl;
}
}
int main()
{
int cho=1;
char buffer[1024];
Schedule *sc=new Schedule();
/*loop for user choice, o to exit*/
while(cho!=0)
{
cout<<"\n1: Add subject\n2: Add Faculty\n3:Add batch\n4:Add Entry in schedule\n5: Show schedule\n0: Exit\n";
cin>>cho;
switch(cho)
{
case 1:
char sub_id[50];
char sub_name[50];
cout<<"\nEnter subject id and name:";
cin>>sub_id>>sub_name;
sprintf(buffer,"insert into subject_list values ('%s','%s');",sub_id,sub_name);
//cout<<buffer;
sc->getResultSet(buffer);
break;
case 2:
char fac_id[50];
char fac_name[50];
cout<<"\nEnter faculty id and name:";
cin>>fac_id>>fac_name;
sprintf(buffer,"insert into faculty_list values ('%s','%s');",fac_id,fac_name);
//cout<<buffer;
sc->getResultSet(buffer);
break;
case 3:
char batch_id[50];
char batch_name[50];
char room_no[50];
cout<<"\nEnter batch id and name:";
cin>>batch_id>>batch_name>>room_no;
sprintf(buffer,"insert into batch_list values ('%s','%s','%s');",batch_id,batch_name,room_no);
//cout<<buffer;
sc->getResultSet(buffer);
break;
case 4:
char day[50];
char time[50];
char type[50];
//char batch_id[50];
//char fac_id[50];
//char sub_id[50];
cout<<"\nEnter Day (Su, Mo...) Time (13:00 24H), Type (LEC, TUT, PC), batch_id, fac_id, sub_id:";
cin>>day>>time>>type>>batch_id>>fac_id>>sub_id;
sprintf(buffer,"insert into routine values ('%s','%s','%s','%s','%s','%s');",day,time,type,batch_id,fac_id,sub_id);
//cout<<buffer;
sc->getResultSet(buffer);
break;
case 5:
cout<<"Specify the batch ID:";
cin>>batch_id;
sc->printSchedule((char*)batch_id);
break;
case 0:
cout<<"\nBye!";
break;
default:
cout<<"\nSelect proper option";
}
}
delete sc;
return 0;
}
MySql Dump file (Current state of the database) [Dump20190815.sql]
-- MySQL dump 10.13 Distrib 5.7.27, for Linux (x86_64)
--
-- Host: localhost Database: schedule
-- ------------------------------------------------------
-- Server version 5.7.27-0ubuntu0.16.04.1
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `batch_list`
--
DROP TABLE IF EXISTS `batch_list`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `batch_list` (
`batch_id` varchar(10) NOT NULL,
`batch_name` varchar(50) NOT NULL,
`room_no` varchar(50) NOT NULL,
PRIMARY KEY (`batch_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `batch_list`
--
LOCK TABLES `batch_list` WRITE;
/*!40000 ALTER TABLE `batch_list` DISABLE KEYS */;
INSERT INTO `batch_list` VALUES ('CUEC218','CUEC218','EC2-01');
/*!40000 ALTER TABLE `batch_list` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `faculty_list`
--
DROP TABLE IF EXISTS `faculty_list`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `faculty_list` (
`fac_id` varchar(10) NOT NULL,
`fac_name` varchar(50) NOT NULL,
PRIMARY KEY (`fac_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `faculty_list`
--
LOCK TABLES `faculty_list` WRITE;
/*!40000 ALTER TABLE `faculty_list` DISABLE KEYS */;
INSERT INTO `faculty_list` VALUES ('FAC1','Dr_Y_Hedley'),('FAC2','Mrs_D_Kondrat'),('FAC3','Dr_D_Croft'),('FAC7','Dr_S_Billings');
/*!40000 ALTER TABLE `faculty_list` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `routine`
--
DROP TABLE IF EXISTS `routine`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `routine` (
`day` varchar(3) NOT NULL,
`time` varchar(20) NOT NULL,
`type` varchar(10) NOT NULL,
`batch_id` varchar(50) NOT NULL,
`fac_id` varchar(50) NOT NULL,
`sub_id` varchar(50) NOT NULL,
PRIMARY KEY (`day`,`time`,`type`,`batch_id`,`fac_id`,`sub_id`),
KEY `batch_id` (`batch_id`),
KEY `fac_id` (`fac_id`),
KEY `sub_id` (`sub_id`),
CONSTRAINT `routine_ibfk_1` FOREIGN KEY (`batch_id`) REFERENCES `batch_list` (`batch_id`),
CONSTRAINT `routine_ibfk_2` FOREIGN KEY (`fac_id`) REFERENCES `faculty_list` (`fac_id`),
CONSTRAINT `routine_ibfk_3` FOREIGN KEY (`sub_id`) REFERENCES `subject_list` (`sub_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `routine`
--
LOCK TABLES `routine` WRITE;
/*!40000 ALTER TABLE `routine` DISABLE KEYS */;
INSERT INTO `routine` VALUES ('Mo','09:00-11:00','LEC','CUEC218','FAC1','4001CEM'),('Mo','11:00-13:00','PC','CUEC218','FAC1','4001CEM'),('Mo','15:00-17:00','LEC','CUEC218','FAC2','4002CEM'),('Th','09:00-11:00','LEC','CUEC218','FAC2','4002CEM'),('Th','13:00-15:00','PC','CUEC218','FAC3','4003CEM'),('Tu','09:00-10:00','LEC','CUEC218','FAC2','4002CEM'),('Tu','11:00-13:00','TUT','CUEC218','FAC3','4003CEM'),('Tu','14:00-18:00','PC','CUEC218','FAC7','4007CEM');
/*!40000 ALTER TABLE `routine` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `subject_list`
--
DROP TABLE IF EXISTS `subject_list`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `subject_list` (
`sub_id` varchar(10) NOT NULL,
`sub_name` varchar(50) NOT NULL,
PRIMARY KEY (`sub_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `subject_list`
--
LOCK TABLES `subject_list` WRITE;
/*!40000 ALTER TABLE `subject_list` DISABLE KEYS */;
INSERT INTO `subject_list` VALUES ('4001CEM','Software_Design'),('4002CEM','Mathematics_for_Computer_Science'),('4003CEM','Object_Oriented_Programming'),('4007CEM','Computer_Science_Activity_Led_Lurning');
/*!40000 ALTER TABLE `subject_list` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;