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
#include "selectClass.h"
int Select::findIndex(string columnName){
for(int i=0; i<columnCount; i++)
{
if(columnName==columnNames[i] ){
return i;
}
}
return -1;
}
Student Select::selectStudent(int id)
{
sqlite3_open("TimeTable.db",&DB);
string selectCommand= "Select * from student where id = ";
selectCommand.append(to_string(id)).append(";");
sqlite3_stmt *statement;
Student student;
student.id=0;
int rc = sqlite3_prepare_v2(DB, selectCommand.c_str(), -1, &statement, NULL);
if (rc != SQLITE_OK) {
cout << "error in getiing data: " << sqlite3_errmsg(DB);
Student errorStudent;
return errorStudent;
}
while ((rc = sqlite3_step(statement)) == SQLITE_ROW) {
student.id = sqlite3_column_int (statement, 0);
student.first_name = string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 1)));
student.last_name = string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 2)));
}
sqlite3_finalize(statement);
sqlite3_close(DB);
return student;
};
Class Select::selectClass(int id)
{
sqlite3_open("TimeTable.db",&DB);
string selectCommand= "Select * from class where id = ";
selectCommand.append(to_string(id)).append(";");
sqlite3_stmt *statement;
Class _class;
_class.id = 0 ;
int rc = sqlite3_prepare_v2(DB, selectCommand.c_str(), -1, &statement, NULL);
if (rc != SQLITE_OK) {
cout << "error in getiing data: " << sqlite3_errmsg(DB);
Class errorClass;
return errorClass;
}
while ((rc = sqlite3_step(statement)) == SQLITE_ROW) {
_class.id = sqlite3_column_int (statement, 0);
_class.name = string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 1)));
_class.module_id = sqlite3_column_int(statement, 2);
_class.room_number = sqlite3_column_int(statement, 3);
_class.class_time = string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 4)));
_class.lecturer_id = sqlite3_column_int(statement, 5);
_class._module=selectmodule(_class.module_id);
_class.room=selectRoom(_class.room_number);
_class.lecturer=selectLecturer(_class.lecturer_id);
}
sqlite3_finalize(statement);
sqlite3_close(DB);
return _class;
};
Lecturer Select::selectLecturer(int id)
{
sqlite3_open("TimeTable.db",&DB);
string selectCommand= "Select * from lecturer where id = ";
selectCommand.append(to_string(id)).append(";");
sqlite3_stmt *statement;
Lecturer lecturer;
lecturer.id=0;
int rc = sqlite3_prepare_v2(DB, selectCommand.c_str(), -1, &statement, NULL);
if (rc != SQLITE_OK) {
cout << "error in getiing data: " << sqlite3_errmsg(DB);
Lecturer errorLecturer;
return errorLecturer;
}
while ((rc = sqlite3_step(statement)) == SQLITE_ROW) {
lecturer.id = sqlite3_column_int (statement, 0);
lecturer.first_name = string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 1)));
lecturer.last_name = string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 2)));
}
sqlite3_finalize(statement);
sqlite3_close(DB);
return lecturer;
};
Room Select::selectRoom(int number)
{
sqlite3_open("TimeTable.db",&DB);
string selectCommand= "Select * from room where number = ";
selectCommand.append(to_string(number)).append(";");
sqlite3_stmt *statement;
Room room;
room.number=0;
int rc = sqlite3_prepare_v2(DB, selectCommand.c_str(), -1, &statement, NULL);
if (rc != SQLITE_OK) {
cout << "error in getiing data: " << sqlite3_errmsg(DB);
Room errorRoom;
return errorRoom;
}
while ((rc = sqlite3_step(statement)) == SQLITE_ROW) {
room.number = sqlite3_column_int (statement, 0);
room.building_name = string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 1)));
}
sqlite3_finalize(statement);
sqlite3_close(DB);
return room;
};
Module Select::selectmodule(int id)
{
sqlite3_open("TimeTable.db",&DB);
string selectCommand= "Select * from module where id = ";
selectCommand.append(to_string(id)).append(";");
sqlite3_stmt *statement;
Module _module;
_module.id=0;
int rc = sqlite3_prepare_v2(DB, selectCommand.c_str(), -1, &statement, NULL);
if (rc != SQLITE_OK) {
cout << "error in getiing data: " << sqlite3_errmsg(DB);
Module errorModule;
return errorModule;
}
while ((rc = sqlite3_step(statement)) == SQLITE_ROW) {
_module.id = sqlite3_column_int (statement, 0);
_module.title = string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 1)));
_module.code = string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 2)));
}
sqlite3_finalize(statement);
sqlite3_close(DB);
return _module;
};
vector<Student> Select::selectStudents(){
sqlite3_open("TimeTable.db",&DB);
string selectCommand= "Select * from student";
// selectCommand.append(to_string(id)).append(";");
sqlite3_stmt *statement;
vector<Student> students;
int rc = sqlite3_prepare_v2(DB, selectCommand.c_str(), -1, &statement, NULL);
if (rc != SQLITE_OK) {
cout << "error in getiing data: " << sqlite3_errmsg(DB);
vector<Student> errorStudent;
return errorStudent;
}
while ((rc = sqlite3_step(statement)) == SQLITE_ROW) {
Student student;
student.id = sqlite3_column_int (statement, 0);
student.first_name = string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 1)));
student.last_name = string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 2)));
students.push_back(student);
}
sqlite3_finalize(statement);
sqlite3_close(DB);
return students;
}
vector<Module> Select::selectModules(){
sqlite3_open("TimeTable.db",&DB);
string selectCommand= "Select * from module";
sqlite3_stmt *statement;
vector<Module> modules;
int rc = sqlite3_prepare_v2(DB, selectCommand.c_str(), -1, &statement, NULL);
if (rc != SQLITE_OK) {
cout << "error in getiing data: " << sqlite3_errmsg(DB);
vector<Module> errorModule;
return errorModule;
}
while ((rc = sqlite3_step(statement)) == SQLITE_ROW) {
Module _module;
_module.id = sqlite3_column_int (statement, 0);
_module.title = string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 1)));
_module.code = string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 2)));
modules.push_back(_module);
}
sqlite3_finalize(statement);
sqlite3_close(DB);
return modules;
}
vector<Class> Select::selectClasses(int module_id){
sqlite3_open("TimeTable.db",&DB);
string selectCommand= "Select * from class where module_id= ";
selectCommand.append(to_string(module_id)).append(";");
sqlite3_stmt *statement;
vector<Class> classes;
int rc = sqlite3_prepare_v2(DB, selectCommand.c_str(), -1, &statement, NULL);
if (rc != SQLITE_OK) {
cout << "error in getiing data: " << sqlite3_errmsg(DB);
vector<Class> errorClass;
return errorClass;
}
while ((rc = sqlite3_step(statement)) == SQLITE_ROW) {
Class _class;
_class.id = sqlite3_column_int (statement, 0);
_class.name = string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 1)));
_class.module_id = sqlite3_column_int(statement, 2);
_class.room_number = sqlite3_column_int(statement, 3);
_class.class_time = string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 4)));
_class.lecturer_id = sqlite3_column_int(statement, 5);
classes.push_back(_class);
}
sqlite3_finalize(statement);
sqlite3_close(DB);
return classes;
}
vector<Room> Select::selectRooms(){
sqlite3_open("TimeTable.db",&DB);
string selectCommand= "Select * from room";
sqlite3_stmt *statement;
vector<Room> rooms;
int rc = sqlite3_prepare_v2(DB, selectCommand.c_str(), -1, &statement, NULL);
if (rc != SQLITE_OK) {
cout << "error in getiing data: " << sqlite3_errmsg(DB);
vector<Room> errorRoom;
return errorRoom;
}
while ((rc = sqlite3_step(statement)) == SQLITE_ROW) {
Room room;
room.number = sqlite3_column_int (statement, 0);
room.building_name = string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 1)));
rooms.push_back(room);
}
sqlite3_finalize(statement);
sqlite3_close(DB);
return rooms;
}
vector<Lecturer> Select::selectLecturers(){
sqlite3_open("TimeTable.db",&DB);
string selectCommand= "Select * from lecturer";
sqlite3_stmt *statement;
vector<Lecturer> lecturers;
int rc = sqlite3_prepare_v2(DB, selectCommand.c_str(), -1, &statement, NULL);
if (rc != SQLITE_OK) {
cout << "error in getiing data: " << sqlite3_errmsg(DB);
vector<Lecturer> errorLecturer;
return errorLecturer;
}
while ((rc = sqlite3_step(statement)) == SQLITE_ROW) {
Lecturer lecturer;
lecturer.id = sqlite3_column_int (statement, 0);
lecturer.first_name = string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 1)));
lecturer.last_name = string(reinterpret_cast<const char*>(sqlite3_column_text(statement, 2)));
lecturers.push_back(lecturer);
}
sqlite3_finalize(statement);
sqlite3_close(DB);
return lecturers;
}
vector<int> Select::getClassIds(int studentID)
{
sqlite3_open("TimeTable.db",&DB);
// select class_id from enrolment where student_id=5;
string selectCommand =" select class_id from enrolment where student_id= ";
selectCommand.append(to_string(studentID)).append(";");
sqlite3_stmt *statement;
vector<int> class_ids;
int rc = sqlite3_prepare_v2(DB, selectCommand.c_str(), -1, &statement, NULL);
if (rc != SQLITE_OK) {
cout << "error in getiing data: " << sqlite3_errmsg(DB);
vector<int> errorClass_id;
return errorClass_id;
}
while ((rc = sqlite3_step(statement)) == SQLITE_ROW) {
int class_id;
class_id = sqlite3_column_int (statement, 0);
class_ids.push_back(class_id);
}
sqlite3_finalize(statement);
sqlite3_close(DB);
return class_ids;
}