-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovidcontroller.cpp
126 lines (126 loc) · 4.24 KB
/
covidcontroller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "covidcontroller.h"
CovidController::CovidController()
{
try{
driver=get_driver_instance();
connection=driver->connect("localhost",
"root", "*******");
connection->setAutoCommit(0);
connection->setSchema("covid_stats");
}catch(SQLException &ex){
cout<<"Exception occurred"<<ex.getErrorCode()<<endl;
}
}
QList<Person> PersonController::getAllStats(){
QList<Covid> list;
PreparedStatement *pstmt=connection->prepareStatement
("SELECT * FROM covid_data");
ResultSet *resultSet=NULL;
try{
resultSet=pstmt->executeQuery();
while(resultSet->next()){
Covid p;
p.setLocation(QString::fromStdString(resultSet->getString
("location")));
p.setActive_Cases(resultSet->getInt("a"));
p.setRecovered(resultSet->getInt("r"));
p.setDeaths(resultSet->getInt("d"));
list.append(p);
}
}catch(SQLException &ex){
cout<<"Exception occurred"<<ex.getErrorCode()<<endl;
}
resultSet->close();
pstmt->close();
delete pstmt;
delete resultSet;
return list;
}
QList<Covid> CovidController::geStatsByLocation(const QString &location){
QList<Covid> list;
ResultSet *resultSet=NULL;
PreparedStatement *pstmt=connection->prepareStatement
("SELECT * FROM covid_data WHERE Location = ");
try{
pstmt->setString(1,lname.toStdString());
resultSet=pstmt->executeQuery();
while(resultSet->next()){
Covid p;
p.setLocation(QString::fromStdString(resultSet->getString
("location")));
p.setActive_Cases(resultSet->getInt("a"));
p.setRecovered(resultSet->getInt("r"));
p.setDeaths(resultSet->getInt("d"));
list.append(p);
}
}catch(SQLException &ex){
cout<<"Exception occurred"<<ex.getErrorCode()<<endl;
}
resultSet->close();
pstmt->close();
delete pstmt;
delete resultSet;
return list;
}
int CovidController::addNewCovid(Covid &p){
int result=0;
PreparedStatement *pstmt=connection->prepareStatement
("INSERT INTO covid_data ( Location, Active_Cases, Recovered,
Deaths )
VALUES (?,?,?,?)");
try{
pstmt->setString(1,p.getLocation().toStdString());
pstmt->setInt(2,p.getActive_Cases());
pstmt->setInt(3,p.getRecovered());
pstmt->setInt(4,p.getDeaths());
result=pstmt->executeUpdate();
connection->commit();
}catch(SQLException &ex){
cout<<"Exception occurred"<<ex.getErrorCode()<<endl;
}
pstmt->close();
delete pstmt;
return result;
}
int CovidController::updateCovid(Covid &p){
int result=0;
PreparedStatement *pstmt=connection->prepareStatement
("UPDATE covid_data SET Location=?, Active_Cases=?, Recovered=?,
Deaths=? WHERE Location=?");
try{
pstmt->setString(1,p.getLocation().toStdString());
pstmt->setInt(2,p.getActive_Cases());
pstmt->setInt(3,p.getRecovered());
pstmt->setInt(4,p.getDeaths());
result=pstmt->executeUpdate();
connection->commit();
}catch(SQLException &ex){
cout<<"Exception occurred"<<ex.getErrorCode()<<endl;
}
pstmt->close();
delete pstmt;
return result;
}
int CovidController::deleteCovid(String Location){
int result=0;
PreparedStatement *pstmt=connection->prepareStatement
("DELETE FROM covid_data WHERE Location=?");
try{
pstmt->setString(1,p.getLocation().toStdString());
result=pstmt->executeUpdate();
connection->commit();
}catch(SQLException &ex){
cout<<"Exception occurred"<<ex.getErrorCode()<<endl;
}
pstmt->close();
delete pstmt;
return result;
}
void CovidController::closeConnection()
{
connection->close();
}
CovidController::~CovidController(){
delete connection;
// delete driver;
}