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?
Timetable-Program/functions.h
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
170 lines (108 sloc)
4.21 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 <string.h> | |
using namespace std; | |
class timetable_db { | |
private: /// SQLite configuration | |
sqlite3 *db; | |
char *zErrMsg; | |
int rc; | |
char* sql; | |
sqlite3_stmt *stmt; | |
static int callback(void *NotUsed, int argc, char **argv, char **azColName) { | |
for(int i = 0; i < argc; i++) { | |
cout << azColName[i] << ": " << argv[i] << endl; | |
} | |
cout << endl; | |
return 0; | |
} | |
// Testing DB errors | |
void errors_db() { | |
if( rc ){ | |
cout << "DB Error: " << sqlite3_errmsg(db) << endl; | |
close_db(); | |
} | |
} | |
public: | |
// Creating DB | |
timetable_db() { | |
rc = sqlite3_open("timetable.db", &db); | |
errors_db(); | |
} | |
// Function to make the tables | |
void create_table() { | |
sql = "CREATE TABLE STUDENT (STUDENT_ID INT PRIMARY KEY, STUDENT_NAME VARCHAR(50));"; | |
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); | |
sql = "CREATE TABLE LECTURER (LECTURER_ID INT PRIMARY KEY, LECTURER_NAME VARCHAR(50));"; | |
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); | |
sql = "CREATE TABLE MODULE (MODULE_ID INT PRIMARY KEY, MODULE_NAME VARCHAR(50));"; | |
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); | |
sql = "CREATE TABLE ROOM (ROOM_ID INT PRIMARY KEY, ROOM_NAME VARCHAR(50));"; | |
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); | |
sql = "CREATE TABLE CLASS (CLASS_ID INT PRIMARY KEY, CLASS_TIME INT, ROOM_ID INT, LECTURER_ID INT, STUDENT_ID INT, MODULE_ID INT);"; | |
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); | |
} | |
// Function to insert data in Students table | |
void insert_student(char* ID, char* name) { | |
char *query = NULL; | |
asprintf(&query, "INSERT INTO STUDENT ('STUDENT_ID', 'STUDENT_NAME') VALUES ('%s', '%s');", ID, name); | |
sqlite3_prepare(db, query, strlen(query), &stmt, NULL); | |
rc = sqlite3_step(stmt); | |
sqlite3_finalize(stmt); | |
free(query); | |
} | |
// Function to insert data in Lecturers table | |
void insert_lecturer(char* ID, char* name) { | |
char *query = NULL; | |
asprintf(&query, "INSERT INTO LECTURER ('LECTURER_ID', 'LECTURER_NAME') VALUES ('%s', '%s');", ID, name); | |
sqlite3_prepare(db, query, strlen(query), &stmt, NULL); | |
rc = sqlite3_step(stmt); | |
sqlite3_finalize(stmt); | |
free(query); | |
} | |
// Function to insert data in Modules table | |
void insert_module(char* ID, char* name) { | |
char *query = NULL; | |
asprintf(&query, "INSERT INTO MODULE ('MODULE_ID', 'MODULE_NAME') VALUES ('%s', '%s');", ID, name); | |
sqlite3_prepare(db, query, strlen(query), &stmt, NULL); | |
rc = sqlite3_step(stmt); | |
sqlite3_finalize(stmt); | |
free(query); | |
} | |
// Function to insert data in Rooms table | |
void insert_room(char* ID, char* name) { | |
char *query = NULL; | |
asprintf(&query, "INSERT INTO ROOM ('ROOM_ID', 'ROOM_NAME') VALUES ('%s', '%s');", ID, name); | |
sqlite3_prepare(db, query, strlen(query), &stmt, NULL); | |
rc = sqlite3_step(stmt); | |
sqlite3_finalize(stmt); | |
free(query); | |
} | |
// Function to insert data in Classes table | |
void insert_class(char* ID, char* class_time, char* room, char* lecturer, char* student, char* module) { | |
char *query = NULL; | |
asprintf(&query, "INSERT INTO CLASS ('CLASS_ID', 'CLASS_TIME', 'ROOM_ID', 'LECTURER_ID', 'STUDENT_ID', 'MODULE_ID') VALUES ('%s', '%s', '%s', '%s', '%s', '%s');", ID, class_time, room, lecturer, student, module); | |
sqlite3_prepare(db, query, strlen(query), &stmt, NULL); | |
rc = sqlite3_step(stmt); | |
sqlite3_finalize(stmt); | |
free(query); | |
} | |
// Function the give DB data as output | |
void show_db() { | |
sql = "SELECT * FROM 'STUDENT';"; | |
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); | |
sql = "SELECT * FROM 'LECTURER';"; | |
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); | |
sql = "SELECT * FROM 'MODULE';"; | |
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); | |
sql = "SELECT * FROM 'ROOM';"; | |
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); | |
sql = "SELECT * FROM 'CLASS';"; | |
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); | |
} | |
// Closing DB connection | |
void close_db() { | |
sqlite3_close(db); | |
} | |
}; |