Permalink
Cannot retrieve contributors at this time
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?
4007CEM-Final/DBClass.h
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
173 lines (173 sloc)
8.79 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <sqlite3.h> | |
#include <libc.h> | |
#include <cstdlib> | |
using namespace std; | |
class TimeTable { | |
private: //Everything in this private section cannot be accessed from outside of the class | |
sqlite3 *db; //Points at SQL Connection | |
char *zErrMsg; //Stores any error messages returned | |
int rc; //Stores any response from SQL creation | |
const char* sql; //Stores SQL commands | |
sqlite3_stmt *stmt; //Stores the combined SQL statement | |
static int callback(void *NotUsed, int argc, char **argv, char **azColName){ | |
for(int i = 0; i<argc; i++){ | |
cout << azColName[i] << ": " << argv[i] << endl; | |
} | |
printf("\n"); | |
return 0; | |
} //Call back function to store returns from submitted statements | |
void TimeTables(){ | |
sql = "CREATE TABLE IF NOT EXISTS TimeTables("\ | |
"Student_ID NOT NULL,"\ | |
"MODULE_ID NOT NULL,"\ | |
"Class_Name NOT NULL,"\ | |
"Lecturer_ID NOT NULL,"\ | |
"Room_Number NOT NULL);"; | |
rc = sqlite3_exec(db, sql, callback, nullptr, &zErrMsg); | |
} // Creates an empty timetable if one doesn't exist | |
void InsertData(){ | |
sql = "INSERT INTO TimeTables ('Student_ID', 'MODULE_ID', 'Class_Name', 'Lecturer_ID', 'Room_Number') VALUES ('9783559','4000CEM','Coding with Python','4332613','402');"\ | |
"INSERT INTO TimeTables ('Student_ID', 'MODULE_ID', 'Class_Name', 'Lecturer_ID', 'Room_Number') VALUES ('9788459','5003CEM','Algorithms','7967999','107');"\ | |
"INSERT INTO TimeTables ('Student_ID', 'MODULE_ID', 'Class_Name', 'Lecturer_ID', 'Room_Number') VALUES ('5733246','5011CEM','Gantt Charts','2809856','930');"\ | |
"INSERT INTO TimeTables ('Student_ID', 'MODULE_ID', 'Class_Name', 'Lecturer_ID', 'Room_Number') VALUES ('5813399','5001CEM','Welcome to websites','2262907','343');"\ | |
"INSERT INTO TimeTables ('Student_ID', 'MODULE_ID', 'Class_Name', 'Lecturer_ID', 'Room_Number') VALUES ('8695248','4007CEM','Databases','8234413','600');"; | |
rc = sqlite3_exec(db, sql, callback, nullptr, &zErrMsg); | |
} // Predefined Data to fill the timetable | |
void InsertDataManual(const char* SID,const char* MID,const char* CN,const char* LID,const char* RN) { | |
const char *query = nullptr; | |
asprintf(const_cast<char **>(&query), "INSERT INTO TimeTables ('Student_ID', 'MODULE_ID', 'Class_Name', 'Lecturer_ID', 'Room_Number') VALUES ('%s','%s','%s','%s','%s');", SID, MID, CN, LID, RN); | |
//Placeholders are used in order to take in a user input straight into the line to run | |
sqlite3_prepare(db, query, strlen(query), &stmt, nullptr); | |
rc = sqlite3_step(stmt); | |
sqlite3_finalize(stmt); | |
} // Allows user to insert their own data | |
void DBErrors() { | |
if (rc) { | |
cout << "Data Base Error:" << sqlite3_errmsg(db) << endl; | |
closeDB(); | |
} | |
} //Checks if there are any SQL errors after running the SQL command | |
//If so the error is printed | |
void GetAll(){ | |
sql = "SELECT * FROM 'TimeTables';"; | |
rc = sqlite3_exec(db, sql, callback, nullptr, &zErrMsg); | |
}//Selects all the values in the timetable | |
//-----------------------------------------------------------------------------------------/// | |
//Isn't functional Yet. This piece of code is created to select a specific timetable | |
void SelectStudent(const char* SID){ | |
const char *query = nullptr; | |
asprintf(const_cast<char **>(&query), "SELECT * FROM TimeTables WHERE Student_ID = '%s';", SID); | |
sqlite3_prepare(db, query, strlen(query), &stmt, nullptr); | |
DBErrors(); | |
rc = sqlite3_step(stmt); | |
DBErrors(); | |
sqlite3_finalize(stmt); | |
DBErrors(); | |
cout << "Added" << endl; | |
} //This function is supposed to allow the user to get personalised timetable | |
void Student(){ | |
string a; | |
cout << "Please enter the student's ID:" << endl; | |
cin >> a; | |
const char* _a = a.c_str(); | |
SelectStudent(_a); | |
} //This function takes the user's input in the form of Student ID | |
//The above two functions function correctly however there is an error that returns from the SQLite connection | |
//that couldn't be understood --- More on this in the video vlog | |
//----------------------------------------------------------------------------------------/// | |
void NewData(){ | |
string a, b, c, d, e; | |
cout << "Please enter the student ID:" << endl; | |
cin >> a; | |
const char* _a = a.c_str(); | |
cout << "Please enter the Module ID: " << endl; | |
cin >> b; | |
const char* _b = b.c_str(); | |
cout << "Please enter the Class Name: " << endl; | |
cin >> c; | |
const char* _c = c.c_str(); | |
cout << "Please enter the Lecturer's ID:" << endl; | |
cin >> d; | |
const char* _d = d.c_str(); | |
cout << "Please enter the Room Number: " << endl; | |
cin >> e; | |
const char* _e = e.c_str(); | |
InsertDataManual(_a,_b,_c,_d,_e); | |
} // Takes user input in order to be run with InsertDataManual in order to be executed in SQL | |
void Choice(int D){ | |
string stmnta = "What would you like to do today? \n (A) Check All Classes \n (B) Add New Class \n (C) EXIT \n (D) Add standard uni library to your directory (Recommended)"; | |
string stmntb = "What would you like to do today? \n (A) Check All Classes \n (B) Add New Class \n (C) EXIT "; | |
string stmntc = "Please give your choice in a single alphabet: "; | |
string choice; | |
if (D != 1){ | |
cout << stmnta << "\n" << stmntc << endl; | |
cin >> choice; | |
if (choice == "a" || choice == "A") { | |
cout << "You have chosen to check all classes:" << endl; | |
GetAll(); | |
Choice(0); | |
}else if (choice == "b" || choice == "B"){ | |
cout << "You have chosen to add new class:" << endl; | |
NewData(); | |
cout << "New Class Added Successfully!" << endl; | |
Choice(0); | |
}else if (choice == "c" || choice == "C"){ | |
cout << "You have chosen to exit. \n Program will shut down shortly. " << endl; | |
sleep(4); | |
closeDB(); | |
exit(1); | |
}else if (choice == "d" || choice == "D"){ | |
cout << "You have chosen to add the standard library to your directory. \n Adding Library:..."; | |
sleep(5); | |
InsertData(); | |
cout << "Library successfully added!" << endl; | |
Choice(1); | |
}else{ | |
cout << "What you entered is not recognized. \n Please try again." << endl; | |
Choice(0); | |
} | |
}else { | |
cout << stmntb << "\n" << stmntc << endl; | |
cin >> choice; | |
if (choice == "a" || choice == "A") { | |
cout << "You have chosen to check all classes:" << endl; | |
GetAll(); | |
Choice(D); | |
}else if (choice == "b" || choice == "B"){ | |
cout << "You have chosen to add new class:" << endl; | |
NewData(); | |
cout << "New Class Added Successfully!" << endl; | |
Choice(D); | |
}else if (choice == "c" || choice == "C"){ | |
cout << "You have chosen to exit. \n Program will shut down shortly. " << endl; | |
sleep(4); | |
closeDB(); | |
exit(1); | |
}else{ | |
cout << "What you entered is not recognized. \n Please try again." << endl; | |
Choice(D); | |
} | |
} | |
} //This is the user interface console that allows the user to "choose their own adventure" | |
public: //Everything in here is what can be used by the main file | |
TimeTable(){ | |
rc = sqlite3_open( "Year2", &db); | |
DBErrors(); | |
TimeTables(); | |
} //Includes the initiation of the Database | |
void Opening(){ | |
//The use of sleep in this function is two fold first is to give the user enough time to read the messages being pushed out | |
//Second it is to give the feeling that this is a heavy program almost at the same speed of any other application | |
cout << "Good Morning!" << endl; | |
sleep(2); | |
cout << "Just 3 seconds while I boot up for your use..." << endl; | |
sleep(7); | |
cout << "Hello There! \n Thank you for being so Patient! " << endl; | |
Choice(0); | |
} //This is the primary interface console that welcomes the user and mimics a realistic scenario | |
void closeDB(){ | |
sqlite3_close(db); | |
} //Closes the Database after user is done with it to avoid injection | |
}; |