forked from 3rd-year-CSE-20/SIS_GUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqliteclass.cpp
194 lines (143 loc) · 5.64 KB
/
sqliteclass.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "sqliteclass.h"
#include "globalDbObject.h"
SQLiteClass::SQLiteClass(QString dbName)
{
db = QSqlDatabase::addDatabase("QSQLITE");
QString dbPath = QDir::currentPath();
dbPath += "/" + dbName;
qDebug() << dbPath;
db.setDatabaseName(dbPath);
if(!db.open()){
qDebug() << "Problem while opening the database";
}
QSqlQuery qry;
query = qry;
}
int SQLiteClass::sql_create(QString table, QStringList columns, QStringList types){
QString sql = QString("CREATE TABLE ") + table + "(";
QString temp = "";
int size = columns.size();
for(int i = 0; i < size; i++){
temp += columns.at(i) + " " + types.at(i) + ",";
}
temp.chop(1);
sql += temp + ");";
qDebug() << sql;
if(!query.exec(sql)){
qDebug() << "error while creating";
}
return 0;
}
int SQLiteClass::sql_select(QString select, QString from, QString where){
QString sql = QString("SELECT ") + select + QString(" FROM ") + from;
if(where == ""){
sql += QString(";");
}
else{
sql += QString(" WHERE ") + where + QString(";");
}
qDebug() << sql;
if(!query.exec(sql)){
qDebug() << "error while selecting";
}
return 0;
}
int SQLiteClass::sql_insert(QString table, QStringList columns, QStringList values){
QString sql = QString("INSERT INTO ") + table + QString(" ( ");
QString co = "";
QString vl = "";
int size = columns.size();
for(int i = 0; i < size; i++){
co += QString("`") + columns.at(i) + QString("`") + QString(",");
vl += QString("'") + values.at(i) + QString("'") + QString(",");
//vl += "?,";
//query.addBindValue(values.at(i));
}
co.chop(1);
vl.chop(1);
sql += co + QString(") ") + QString("VALUES(") + vl + QString(");");
qDebug() << sql;
if(!query.exec(sql)){
qDebug() << "error while inserting";
}
return query.numRowsAffected();
}
int SQLiteClass::sql_update(QString table, QStringList columns, QStringList values, QString where){
QString sql = QString("UPDATE `") + table + QString("` SET ") ;
QString temp = "";
int size = columns.size();
for(int i = 0; i < size; i++){
temp = temp + QString("`") + columns.at(i) + QString("`") + QString("=") + "'" + values.at(i) + "'" + QString(",");
//query.addBindValue(values.at(i));
}
temp.chop(1);
sql = sql + temp + QString(" WHERE ") + where;
//qDebug() << sql;
if(!query.exec(sql)){
qDebug() << "error while updating";
}
return query.numRowsAffected();
}
int SQLiteClass::sql_delete(QString table, QString where){
QString sql = QString("DELETE ") + QString("FROM ") + table + QString(" WHERE ") + where ;
//qDebug() << sql;
QSqlQuery qry;
qry.exec(sql);
query = qry;
return 0;
}
int SQLiteClass::sql_print(){
while (query.next()) {
int id = query.value(0).toInt();
QString name = query.value(1).toString();
int department = query.value(2).toInt();
qDebug() << id << name << department;
}
return 0;
}
QSqlQuery SQLiteClass::sql_getQuery(){
return query;
}
int SQLiteClass::sql_close(){
db.close();
return 0;
}
void databaseInitialization(){
// students table
QString students_table = "students";
QStringList students_columns = {"id","first_name","last_name", "gendre", "picture",
"birth_date", "address", "college_id", "password", "academic_year", "department", "GPA"};
QStringList students_types = {"INTEGER PRIMARY KEY AUTOINCREMENT", "TEXT", "TEXT", "TEXT", "TEXT",
"TEXT", "TEXT", "TEXT", "TEXT", "TEXT", "TEXT", "TEXT"};
// courses table
QString courses_table = "courses";
QStringList courses_columns = {"id","name"};
QStringList courses_types = {"INTEGER PRIMARY KEY AUTOINCREMENT", "TEXT"};
// courses / students table
QString courses_students_table = "courses_students";
QStringList courses_students_columns = {"course_id", "student_id"};
QStringList courses_students_types = {"INTEGER", "INTEGER"};
// staff members table
QString staff_table = "staff_members";
QStringList staff_columns = {"id","first_name","last_name", "gendre", "picture",
"birth_date", "address", "college_id", "password", "degree", "department"};
QStringList staff_types = {"INTEGER PRIMARY KEY AUTOINCREMENT", "TEXT", "TEXT", "TEXT", "TEXT",
"TEXT", "TEXT", "TEXT", "TEXT", "TEXT", "TEXT"};
// courses / staff members table
QString courses_staff_table = "courses_staff_members";
QStringList courses_staff_columns = {"course_id", "staff_member_id"};
QStringList courses_staff_types = {"INTEGER", "INTEGER"};
// admins table
QString admins_table = "admins";
QStringList admins_columns = {"id","first_name","last_name", "gendre", "picture",
"birth_date", "address", "college_id", "password"};
QStringList admins_types = {"INTEGER PRIMARY KEY AUTOINCREMENT", "TEXT", "TEXT", "TEXT", "TEXT",
"TEXT", "TEXT", "TEXT", "TEXT"};
// creating tables
SQLiteDb.sql_create(students_table, students_columns, students_types);
SQLiteDb.sql_create(courses_table, courses_columns, courses_types);
SQLiteDb.sql_create(courses_students_table, courses_students_columns, courses_students_types);
SQLiteDb.sql_create(staff_table, staff_columns, staff_types);
SQLiteDb.sql_create(courses_staff_table, courses_staff_columns, courses_staff_types);
SQLiteDb.sql_create(admins_table, admins_columns, admins_types);
}