Skip to content
Permalink
master
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
{\rtf1\ansi\ansicpg1252\cocoartf2580
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fnil\fcharset0 Menlo-Regular;\f1\fnil\fcharset0 Menlo-Bold;}
{\colortbl;\red255\green255\blue255;\red68\green99\blue135;\red255\green255\blue255;\red38\green38\blue38;
\red16\green121\blue2;\red170\green0\blue83;\red134\green101\blue3;\red117\green117\blue117;\red38\green32\blue135;
\red9\green80\blue173;\red0\green0\blue213;\red255\green236\blue236;\red83\green83\blue83;}
{\*\expandedcolortbl;;\cssrgb\c33333\c46667\c60000;\cssrgb\c100000\c100000\c100000;\cssrgb\c20000\c20000\c20000;
\cssrgb\c0\c53333\c0;\cssrgb\c73333\c0\c40000;\cssrgb\c60000\c46667\c0;\cssrgb\c53333\c53333\c53333;\cssrgb\c20000\c20000\c60000;
\cssrgb\c0\c40000\c73333;\cssrgb\c0\c0\c86667;\cssrgb\c100000\c94118\c94118;\cssrgb\c40000\c40000\c40000;}
\paperw11900\paperh16840\margl1440\margr1440\vieww38200\viewh21600\viewkind0
\deftab720
\pard\pardeftab720\partightenfactor0
\f0\fs26 \cf2 \cb3 \expnd0\expndtw0\kerning0
Below is the code in my header File:\
\
#include <iostream>\cf4 \
\cf2 #include <string>\cf4 \
\cf2 #include <sqlite3.h>\cf4 \
\cf2 #include <libc.h>\cf4 \
\cf2 #include <cstdlib>\cf4 \
\pard\pardeftab720\partightenfactor0
\f1\b \cf5 using
\f0\b0 \cf4
\f1\b \cf5 namespace
\f0\b0 \cf4 std;\
\f1\b \cf5 class
\f0\b0 \cf4
\f1\b \cf6 TimeTable
\f0\b0 \cf4 \{\
\f1\b \cf7 private:
\f0\b0 \cf4 \cf8 //Everything in this private section cannot be accessed from outside of the class\cf4 \
sqlite3 *db; \cf8 //Points at SQL Connection\cf4 \
\f1\b \cf9 char
\f0\b0 \cf4 *zErrMsg; \cf8 //Stores any error messages returned\cf4 \
\f1\b \cf9 int
\f0\b0 \cf4 rc; \cf8 //Stores any response from SQL creation\cf4 \
\f1\b \cf5 const
\f0\b0 \cf4
\f1\b \cf9 char
\f0\b0 \cf4 * sql; \cf8 //Stores SQL commands\cf4 \
sqlite3_stmt *stmt; \cf8 //Stores the combined SQL statement\cf4 \
\f1\b \cf5 static
\f0\b0 \cf4
\f1\b \cf9 int
\f0\b0 \cf4
\f1\b \cf10 callback
\f0\b0 \cf4 (
\f1\b \cf9 void
\f0\b0 \cf4 *NotUsed,
\f1\b \cf9 int
\f0\b0 \cf4 argc,
\f1\b \cf9 char
\f0\b0 \cf4 **argv,
\f1\b \cf9 char
\f0\b0 \cf4 **azColName)\{\
\f1\b \cf5 for
\f0\b0 \cf4 (
\f1\b \cf9 int
\f0\b0 \cf4 i =
\f1\b \cf11 0
\f0\b0 \cf4 ; i<argc; i++)\{\
cout << azColName[i] << \cb12 ": "\cb3 << argv[i] << endl;\
\}\
printf(\cb12 "
\f1\b \cf13 \\n
\f0\b0 \cf4 "\cb3 );\
\f1\b \cf5 return
\f0\b0 \cf4
\f1\b \cf11 0
\f0\b0 \cf4 ;\
\} \cf8 //Call back function to store returns from submitted statements\cf4 \
\f1\b \cf9 void
\f0\b0 \cf4
\f1\b \cf10 TimeTables
\f0\b0 \cf4 ()\{\
sql = \cb12 "CREATE TABLE IF NOT EXISTS TimeTables("\cb3 \\\
\cb12 "Student_ID NOT NULL,"\cb3 \\\
\cb12 "MODULE_ID NOT NULL,"\cb3 \\\
\cb12 "Class_Name NOT NULL,"\cb3 \\\
\cb12 "Lecturer_ID NOT NULL,"\cb3 \\\
\cb12 "Room_Number NOT NULL);"\cb3 ;\
rc = sqlite3_exec(db, sql, callback, nullptr, &zErrMsg);\
\} \cf8 // Creates an empty timetable if one doesn't exist\cf4 \
\f1\b \cf9 void
\f0\b0 \cf4
\f1\b \cf10 InsertData
\f0\b0 \cf4 ()\{\
sql = \cb12 "INSERT INTO TimeTables ('Student_ID', 'MODULE_ID', 'Class_Name', 'Lecturer_ID', 'Room_Number') VALUES ('9783559','4000CEM','Coding with Python','4332613','402');"\cb3 \\\
\cb12 "INSERT INTO TimeTables ('Student_ID', 'MODULE_ID', 'Class_Name', 'Lecturer_ID', 'Room_Number') VALUES ('9788459','5003CEM','Algorithms','7967999','107');"\cb3 \\\
\cb12 "INSERT INTO TimeTables ('Student_ID', 'MODULE_ID', 'Class_Name', 'Lecturer_ID', 'Room_Number') VALUES ('5733246','5011CEM','Gantt Charts','2809856','930');"\cb3 \\\
\cb12 "INSERT INTO TimeTables ('Student_ID', 'MODULE_ID', 'Class_Name', 'Lecturer_ID', 'Room_Number') VALUES ('5813399','5001CEM','Welcome to websites','2262907','343');"\cb3 \\\
\cb12 "INSERT INTO TimeTables ('Student_ID', 'MODULE_ID', 'Class_Name', 'Lecturer_ID', 'Room_Number') VALUES ('8695248','4007CEM','Databases','8234413','600');"\cb3 ;\
rc = sqlite3_exec(db, sql, callback, nullptr, &zErrMsg);\
\} \cf8 // Predefined Data to fill the timetable\cf4 \
\f1\b \cf9 void
\f0\b0 \cf4
\f1\b \cf10 InsertDataManual
\f0\b0 \cf4 (
\f1\b \cf5 const
\f0\b0 \cf4
\f1\b \cf9 char
\f0\b0 \cf4 * SID,
\f1\b \cf5 const
\f0\b0 \cf4
\f1\b \cf9 char
\f0\b0 \cf4 * MID,
\f1\b \cf5 const
\f0\b0 \cf4
\f1\b \cf9 char
\f0\b0 \cf4 * CN,
\f1\b \cf5 const
\f0\b0 \cf4
\f1\b \cf9 char
\f0\b0 \cf4 * LID,
\f1\b \cf5 const
\f0\b0 \cf4
\f1\b \cf9 char
\f0\b0 \cf4 * RN) \{\
\f1\b \cf5 const
\f0\b0 \cf4
\f1\b \cf9 char
\f0\b0 \cf4 *query = nullptr;\
asprintf(
\f1\b \cf5 const_cast
\f0\b0 \cf4 <
\f1\b \cf9 char
\f0\b0 \cf4 **>(&query), \cb12 "INSERT INTO TimeTables ('Student_ID', 'MODULE_ID', 'Class_Name', 'Lecturer_ID', 'Room_Number') VALUES ('%s','%s','%s','%s','%s');"\cb3 , SID, MID, CN, LID, RN);\
\cf8 //Placeholders are used in order to take in a user input straight into the line to run\cf4 \
sqlite3_prepare(db, query, strlen(query), &stmt, nullptr);\
rc = sqlite3_step(stmt);\
sqlite3_finalize(stmt);\
\} \cf8 // Allows user to insert their own data\cf4 \
\f1\b \cf9 void
\f0\b0 \cf4
\f1\b \cf10 DBErrors
\f0\b0 \cf4 () \{\
\f1\b \cf5 if
\f0\b0 \cf4 (rc) \{\
cout << \cb12 "Data Base Error:"\cb3 << sqlite3_errmsg(db) << endl;\
closeDB();\
\}\
\} \cf8 //Checks if there are any SQL errors after running the SQL command\cf4 \
\cf8 //If so the error is printed\cf4 \
\f1\b \cf9 void
\f0\b0 \cf4
\f1\b \cf10 GetAll
\f0\b0 \cf4 ()\{\
sql = \cb12 "SELECT * FROM 'TimeTables';"\cb3 ;\
rc = sqlite3_exec(db, sql, callback, nullptr, &zErrMsg);\
\}\cf8 //Selects all the values in the timetable\cf4 \
\cf8 //-----------------------------------------------------------------------------------------///\cf4 \
\cf8 //Isn't functional Yet. This piece of code is created to select a specific timetable\cf4 \
\f1\b \cf9 void
\f0\b0 \cf4
\f1\b \cf10 SelectStudent
\f0\b0 \cf4 (
\f1\b \cf5 const
\f0\b0 \cf4
\f1\b \cf9 char
\f0\b0 \cf4 * SID)\{\
\f1\b \cf5 const
\f0\b0 \cf4
\f1\b \cf9 char
\f0\b0 \cf4 *query = nullptr;\
asprintf(
\f1\b \cf5 const_cast
\f0\b0 \cf4 <
\f1\b \cf9 char
\f0\b0 \cf4 **>(&query), \cb12 "SELECT * FROM TimeTables WHERE Student_ID = '%s';"\cb3 , SID);\
sqlite3_prepare(db, query, strlen(query), &stmt, nullptr);\
DBErrors();\
rc = sqlite3_step(stmt);\
DBErrors();\
sqlite3_finalize(stmt);\
DBErrors();\
cout << \cb12 "Added"\cb3 << endl;\
\} \cf8 //This function is supposed to allow the user to get personalised timetable\cf4 \
\f1\b \cf9 void
\f0\b0 \cf4
\f1\b \cf10 Student
\f0\b0 \cf4 ()\{\
string a;\
cout << \cb12 "Please enter the student's ID:"\cb3 << endl;\
cin >> a;\
\f1\b \cf5 const
\f0\b0 \cf4
\f1\b \cf9 char
\f0\b0 \cf4 * _a = a.c_str();\
SelectStudent(_a);\
\} \cf8 //This function takes the user's input in the form of Student ID\cf4 \
\cf8 //The above two functions function correctly however there is an error that returns from the SQLite connection\cf4 \
\cf8 //that couldn't be understood --- More on this in the video vlog\cf4 \
\cf8 //----------------------------------------------------------------------------------------///\cf4 \
\f1\b \cf9 void
\f0\b0 \cf4
\f1\b \cf10 NewData
\f0\b0 \cf4 ()\{\
string a, b, c, d, e;\
cout << \cb12 "Please enter the student ID:"\cb3 << endl;\
cin >> a;\
\f1\b \cf5 const
\f0\b0 \cf4
\f1\b \cf9 char
\f0\b0 \cf4 * _a = a.c_str();\
cout << \cb12 "Please enter the Module ID: "\cb3 << endl;\
cin >> b;\
\f1\b \cf5 const
\f0\b0 \cf4
\f1\b \cf9 char
\f0\b0 \cf4 * _b = b.c_str();\
cout << \cb12 "Please enter the Class Name: "\cb3 << endl;\
cin >> c;\
\f1\b \cf5 const
\f0\b0 \cf4
\f1\b \cf9 char
\f0\b0 \cf4 * _c = c.c_str();\
cout << \cb12 "Please enter the Lecturer's ID:"\cb3 << endl;\
cin >> d;\
\f1\b \cf5 const
\f0\b0 \cf4
\f1\b \cf9 char
\f0\b0 \cf4 * _d = d.c_str();\
cout << \cb12 "Please enter the Room Number: "\cb3 << endl;\
cin >> e;\
\f1\b \cf5 const
\f0\b0 \cf4
\f1\b \cf9 char
\f0\b0 \cf4 * _e = e.c_str();\
InsertDataManual(_a,_b,_c,_d,_e);\
\} \cf8 // Takes user input in order to be run with InsertDataManual in order to be executed in SQL\cf4 \
\f1\b \cf9 void
\f0\b0 \cf4
\f1\b \cf10 Choice
\f0\b0 \cf4 (
\f1\b \cf9 int
\f0\b0 \cf4 D)\{\
string stmnta = \cb12 "What would you like to do today?
\f1\b \cf13 \\n
\f0\b0 \cf4 (A) Check All Classes
\f1\b \cf13 \\n
\f0\b0 \cf4 (B) Add New Class
\f1\b \cf13 \\n
\f0\b0 \cf4 (C) EXIT
\f1\b \cf13 \\n
\f0\b0 \cf4 (D) Add standard uni library to your directory (Recommended)"\cb3 ;\
string stmntb = \cb12 "What would you like to do today?
\f1\b \cf13 \\n
\f0\b0 \cf4 (A) Check All Classes
\f1\b \cf13 \\n
\f0\b0 \cf4 (B) Add New Class
\f1\b \cf13 \\n
\f0\b0 \cf4 (C) EXIT "\cb3 ;\
string stmntc = \cb12 "Please give your choice in a single alphabet: "\cb3 ;\
string choice;\
\f1\b \cf5 if
\f0\b0 \cf4 (D !=
\f1\b \cf11 1
\f0\b0 \cf4 )\{\
cout << stmnta << \cb12 "
\f1\b \cf13 \\n
\f0\b0 \cf4 "\cb3 << stmntc << endl;\
cin >> choice;\
\f1\b \cf5 if
\f0\b0 \cf4 (choice == \cb12 "a"\cb3 || choice == \cb12 "A"\cb3 ) \{\
cout << \cb12 "You have chosen to check all classes:"\cb3 << endl;\
GetAll();\
Choice(
\f1\b \cf11 0
\f0\b0 \cf4 );\
\}
\f1\b \cf5 else
\f0\b0 \cf4
\f1\b \cf5 if
\f0\b0 \cf4 (choice == \cb12 "b"\cb3 || choice == \cb12 "B"\cb3 )\{\
cout << \cb12 "You have chosen to add new class:"\cb3 << endl;\
NewData();\
cout << \cb12 "New Class Added Successfully!"\cb3 << endl;\
Choice(
\f1\b \cf11 0
\f0\b0 \cf4 );\
\}
\f1\b \cf5 else
\f0\b0 \cf4
\f1\b \cf5 if
\f0\b0 \cf4 (choice == \cb12 "c"\cb3 || choice == \cb12 "C"\cb3 )\{\
cout << \cb12 "You have chosen to exit.
\f1\b \cf13 \\n
\f0\b0 \cf4 Program will shut down shortly. "\cb3 << endl;\
sleep(
\f1\b \cf11 4
\f0\b0 \cf4 );\
closeDB();\
exit(
\f1\b \cf11 1
\f0\b0 \cf4 );\
\}
\f1\b \cf5 else
\f0\b0 \cf4
\f1\b \cf5 if
\f0\b0 \cf4 (choice == \cb12 "d"\cb3 || choice == \cb12 "D"\cb3 )\{\
cout << \cb12 "You have chosen to add the standard library to your directory.
\f1\b \cf13 \\n
\f0\b0 \cf4 Adding Library:..."\cb3 ;\
sleep(
\f1\b \cf11 5
\f0\b0 \cf4 );\
InsertData();\
cout << \cb12 "Library successfully added!"\cb3 << endl;\
Choice(
\f1\b \cf11 1
\f0\b0 \cf4 );\
\}
\f1\b \cf5 else
\f0\b0 \cf4 \{\
cout << \cb12 "What you entered is not recognized.
\f1\b \cf13 \\n
\f0\b0 \cf4 Please try again."\cb3 << endl;\
Choice(
\f1\b \cf11 0
\f0\b0 \cf4 );\
\}\
\}
\f1\b \cf5 else
\f0\b0 \cf4 \{\
cout << stmntb << \cb12 "
\f1\b \cf13 \\n
\f0\b0 \cf4 "\cb3 << stmntc << endl;\
cin >> choice;\
\f1\b \cf5 if
\f0\b0 \cf4 (choice == \cb12 "a"\cb3 || choice == \cb12 "A"\cb3 ) \{\
cout << \cb12 "You have chosen to check all classes:"\cb3 << endl;\
GetAll();\
Choice(D);\
\}
\f1\b \cf5 else
\f0\b0 \cf4
\f1\b \cf5 if
\f0\b0 \cf4 (choice == \cb12 "b"\cb3 || choice == \cb12 "B"\cb3 )\{\
cout << \cb12 "You have chosen to add new class:"\cb3 << endl;\
NewData();\
cout << \cb12 "New Class Added Successfully!"\cb3 << endl;\
Choice(D);\
\}
\f1\b \cf5 else
\f0\b0 \cf4
\f1\b \cf5 if
\f0\b0 \cf4 (choice == \cb12 "c"\cb3 || choice == \cb12 "C"\cb3 )\{\
cout << \cb12 "You have chosen to exit.
\f1\b \cf13 \\n
\f0\b0 \cf4 Program will shut down shortly. "\cb3 << endl;\
sleep(
\f1\b \cf11 4
\f0\b0 \cf4 );\
closeDB();\
exit(
\f1\b \cf11 1
\f0\b0 \cf4 );\
\}
\f1\b \cf5 else
\f0\b0 \cf4 \{\
cout << \cb12 "What you entered is not recognized.
\f1\b \cf13 \\n
\f0\b0 \cf4 Please try again."\cb3 << endl;\
Choice(D);\
\}\
\}\
\} \cf8 //This is the user interface console that allows the user to "choose their own adventure"\cf4 \
\f1\b \cf7 public:
\f0\b0 \cf4 \cf8 //Everything in here is what can be used by the main file\cf4 \
TimeTable()\{\
rc = sqlite3_open( \cb12 "Year2"\cb3 , &db);\
DBErrors();\
TimeTables();\
\} \cf8 //Includes the initiation of the Database\cf4 \
\f1\b \cf9 void
\f0\b0 \cf4 Opening()\{\
\cf8 //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\cf4 \
\cf8 //Second it is to give the feeling that this is a heavy program almost at the same speed of any other application\cf4 \
cout << \cb12 "Good Morning!"\cb3 << endl;\
sleep(
\f1\b \cf11 2
\f0\b0 \cf4 );\
cout << \cb12 "Just 3 seconds while I boot up for your use..."\cb3 << endl;\
sleep(
\f1\b \cf11 7
\f0\b0 \cf4 );\
cout << \cb12 "Hello There!
\f1\b \cf13 \\n
\f0\b0 \cf4 Thank you for being so Patient! "\cb3 << endl;\
Choice(
\f1\b \cf11 0
\f0\b0 \cf4 );\
\} \cf8 //This is the primary interface console that welcomes the user and mimics a realistic scenario\cf4 \
\f1\b \cf9 void
\f0\b0 \cf4 closeDB()\{\
sqlite3_close(db);\
\} \cf8 //Closes the Database after user is done with it to avoid injection\cf4 \
\};\
\
Below is the Code in my main.cpp file:\
\
\pard\pardeftab720\partightenfactor0
\cf2 #include "DBClass.h"\cf4 \
\pard\pardeftab720\partightenfactor0
\f1\b \cf9 int
\f0\b0 \cf4
\f1\b \cf10 main
\f0\b0 \cf4 () \{\
TimeTable Year2; \cf8 //Initiates the database\cf4 \
Year2.Opening(); \cf8 //runs the interface function\cf4 \
\f1\b \cf5 return
\f0\b0 \cf4
\f1\b \cf11 0
\f0\b0 \cf4 ;\
\}\
\
\
\
This is all the code I have written in this project\
}