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_Resit/queries.h
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
164 lines (121 sloc)
3.05 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 <array> | |
#include <string> | |
#include "libsqlite.hpp" | |
using namespace std; | |
//These sqlite codes were new to me so i used the following project for help https://github.coventry.ac.uk/milesd7/122COM-Project/blob/master/DataBase.h | |
int InsertLecturer(int id, string Name) | |
{ | |
try | |
{ | |
sqlite::sqlite db("database.db"); //open database | |
auto q1 = db.get_statement(); //create cursor | |
auto q2 = db.get_statement(); //create cursor | |
q1->set_sql("INSERT INTO Lecturers ( LectID, LectName) " | |
"VALUES (?, ?);"); // insert values to the DB | |
q1->prepare(); | |
q1->bind(1, id); | |
q1->bind(2, Name); | |
q1->step(); | |
} | |
catch (sqlite::exception e) | |
{ | |
cerr << e.what() << endl; | |
return 1; | |
} | |
} | |
int InsertStudent(int id, string Name, string class1) | |
{ | |
try | |
{ | |
sqlite::sqlite db("database.db"); //open database | |
auto q1 = db.get_statement(); //create cursor | |
auto q2 = db.get_statement(); //create cursor | |
q1->set_sql("INSERT INTO Students ( StudentID, StudentName) " | |
"VALUES (?, ?);"); // insert values to the DB | |
q1->prepare(); | |
q1->bind(1, id); | |
q1->bind(2, Name); | |
q1->step(); | |
} | |
catch (sqlite::exception e) | |
{ | |
cerr << e.what() << endl; | |
return 1; | |
} | |
} | |
int showLecturers() | |
{ | |
try | |
{ | |
sqlite::sqlite db("database.db"); // open database | |
auto us = db.get_statement(); | |
auto bo = db.get_statement(); | |
us->set_sql("SELECT * FROM Lecturers"); // set query | |
us->prepare(); | |
while (us->step()) | |
{ | |
if (us->get_int(0) < 10) | |
{ // if datas from DB is less than 10 , spaces between id and name is 5 | |
cout << " " << us->get_int(0) << " " << us->get_text(1) << endl; | |
} | |
else | |
{ //// if datas from DB is more than 10 , spaces between id and name is 4.. | |
cout << " " << us->get_int(0) << " " << us->get_text(1) << endl; | |
} | |
} | |
} | |
catch (sqlite::exception e) | |
{ | |
cerr << e.what() << endl; | |
return 1; | |
} | |
} | |
int showStudents() | |
{ | |
try | |
{ | |
sqlite::sqlite db("database.db"); // open database | |
auto q1 = db.get_statement(); | |
q1->set_sql("SELECT * FROM Student"); // set query | |
q1->prepare(); | |
while (q1->step()) | |
{ | |
if (q1->get_int(0) < 5) | |
{ | |
cout << " " << q1->get_int(0) << " " << q1->get_text(1) << " " << q1->get_text(2) << endl; | |
} | |
else | |
{ | |
cout << " " << q1->get_int(0) << " " << q1->get_text(1) << " " << q1->get_text(2) << endl; | |
} | |
} | |
} | |
catch (sqlite::exception e) | |
{ | |
cerr << e.what() << endl; | |
return 1; | |
} | |
} | |
void showModuleRoom(int moduleID) | |
{ | |
try | |
{ | |
{ | |
sqlite::sqlite db("database.db"); | |
auto curs = db.get_statement(); | |
curs->set_sql("SELECT * FROM Modules WHERE ModuleID = (?)"); | |
curs->prepare(); | |
curs->bind(1, ModuleID); | |
while (curs->step()) | |
{ | |
cout << " " << curs->get_text(0) << " " << curs->get_text(2) << endl; | |
} | |
} | |
} | |
catch (sqlite::exception e) | |
{ | |
cerr << e.what() << endl; | |
return 1; | |
} | |
} |