-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdbmanager.cpp
154 lines (129 loc) · 2.96 KB
/
dbmanager.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
#include <dbmanager.h>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlRecord>
#include <QDebug>
DbManager::DbManager(const QString &path)
{
m_db = QSqlDatabase::addDatabase("QSQLITE");
m_db.setDatabaseName(path);
if (!m_db.open())
{
qDebug() << "Error: connection with database fail";
}
else
{
qDebug() << "Database: connection ok";
}
}
DbManager::~DbManager()
{
if (m_db.isOpen())
{
m_db.close();
}
}
bool DbManager::isOpen() const
{
return m_db.isOpen();
}
bool DbManager::createTable()
{
bool success = false;
QSqlQuery query;
query.prepare("CREATE TABLE people(id INTEGER PRIMARY KEY, name TEXT);");
if (!query.exec())
{
qDebug() << "Couldn't create the table 'people': one might already exist.";
success = false;
}
return success;
}
bool DbManager::addPerson(const QString& name)
{
bool success = false;
if (!name.isEmpty())
{
QSqlQuery queryAdd;
queryAdd.prepare("INSERT INTO people (name) VALUES (:name)");
queryAdd.bindValue(":name", name);
if(queryAdd.exec())
{
success = true;
}
else
{
qDebug() << "add person failed: " << queryAdd.lastError();
}
}
else
{
qDebug() << "add person failed: name cannot be empty";
}
return success;
}
bool DbManager::removePerson(const QString& name)
{
bool success = false;
if (personExists(name))
{
QSqlQuery queryDelete;
queryDelete.prepare("DELETE FROM people WHERE name = (:name)");
queryDelete.bindValue(":name", name);
success = queryDelete.exec();
if(!success)
{
qDebug() << "remove person failed: " << queryDelete.lastError();
}
}
else
{
qDebug() << "remove person failed: person doesnt exist";
}
return success;
}
void DbManager::printAllPersons() const
{
qDebug() << "Persons in db:";
QSqlQuery query("SELECT * FROM people");
int idName = query.record().indexOf("name");
while (query.next())
{
QString name = query.value(idName).toString();
qDebug() << "===" << name;
}
}
bool DbManager::personExists(const QString& name) const
{
bool exists = false;
QSqlQuery checkQuery;
checkQuery.prepare("SELECT name FROM people WHERE name = (:name)");
checkQuery.bindValue(":name", name);
if (checkQuery.exec())
{
if (checkQuery.next())
{
exists = true;
}
}
else
{
qDebug() << "person exists failed: " << checkQuery.lastError();
}
return exists;
}
bool DbManager::removeAllPersons()
{
bool success = false;
QSqlQuery removeQuery;
removeQuery.prepare("DELETE FROM people");
if (removeQuery.exec())
{
success = true;
}
else
{
qDebug() << "remove all persons failed: " << removeQuery.lastError();
}
return success;
}