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?
Resit/database.h
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
216 lines (88 sloc)
2.82 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> #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; | |
} | |
} |