Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
935 additions
and
207 deletions.
There are no files selected for viewing
216
database.h
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
#include <iostream> #took some help from https://github.coventry.ac.uk/milesd7/122COM-Project/blob/master/DataBase.h | ||
|
||
#include <array> | ||
|
||
#include <string> | ||
|
||
#include "libsqlite.hpp" | ||
|
||
using namespace std; | ||
|
||
int UploadLecturer(int id, string Name){ | ||
|
||
try | ||
|
||
{ | ||
|
||
sqlite::sqlite db( "uni_info.db" ); //open database | ||
|
||
|
||
|
||
auto q1 = db.get_statement();//create cursor | ||
|
||
auto q2 = db.get_statement();//create cursor | ||
|
||
|
||
|
||
q1->set_sql( "INSERT INTO Lecturers ( Lect_ID, Lect_name) " | ||
|
||
"VALUES (?, ?);" );// insert into DB | ||
|
||
q1->prepare(); | ||
|
||
|
||
|
||
q1->bind( 1, id ); | ||
|
||
q1->bind( 2, Name ); | ||
|
||
|
||
|
||
q1->step(); | ||
|
||
|
||
} | ||
|
||
|
||
|
||
catch( sqlite::exception e ) | ||
|
||
{ | ||
|
||
cerr << e.what() << endl; | ||
|
||
return 1; | ||
|
||
} | ||
|
||
} | ||
int UploadStudent(int id, string Name, string class1){ | ||
|
||
try | ||
|
||
{ | ||
|
||
sqlite::sqlite db( "uni_info.db" ); //open database | ||
|
||
|
||
|
||
auto q1 = db.get_statement();//create cursor | ||
|
||
auto q2 = db.get_statement();//create cursor | ||
|
||
|
||
|
||
q1->set_sql( "INSERT INTO Student ( Stu_ID, Student_Name, Class) " | ||
|
||
"VALUES (?, ?, ?);" );// insert into DB | ||
|
||
q1->prepare(); | ||
|
||
|
||
|
||
q1->bind( 1, id ); | ||
|
||
q1->bind( 2, Name ); | ||
|
||
q1->bind( 3, class1 ); | ||
|
||
q1->step(); | ||
|
||
|
||
} | ||
|
||
|
||
|
||
catch( sqlite::exception e ) | ||
|
||
{ | ||
|
||
cerr << e.what() << endl; | ||
|
||
return 1; | ||
|
||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
int printLecturers(){ | ||
|
||
try | ||
|
||
{ | ||
|
||
sqlite::sqlite db( "uni_info.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 printStudents(){ | ||
|
||
try | ||
|
||
{ | ||
|
||
sqlite::sqlite db( "uni_info.db" ); // open database | ||
|
||
|
||
|
||
auto us = db.get_statement(); | ||
|
||
auto bo = db.get_statement(); | ||
|
||
|
||
|
||
us->set_sql( "SELECT * FROM Student" );// 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) << " " << us->get_text(2)<<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) <<" " << us->get_text(2)<<endl; | ||
|
||
|
||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
catch( sqlite::exception e ){ | ||
|
||
cerr << e.what() << endl; | ||
|
||
return 1; | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.