-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_settings.cpp
More file actions
72 lines (53 loc) · 1.45 KB
/
Copy pathdb_settings.cpp
File metadata and controls
72 lines (53 loc) · 1.45 KB
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
#include <QSqlQuery>
#include <QSqlError>
#include <QVariant>
#include <QDebug>
#include "db_settings.h"
void DbTools::execWithCheck(QSqlQuery& qu) {
if (!qu.exec())
qDebug() << qu.lastError().text();
}
void DbTools::execWithCheck(const QString& txt) {
QSqlQuery qu;
qu.prepare(txt);
execWithCheck(qu);
}
static void initTableSettings() {
static bool already = false;
if (already) return;
already = true;
QSqlQuery qu;
qu.prepare(
"create table if not exists SETTINGS ("
" name text primary key, "
" value text)");
DbTools::execWithCheck(qu);
}
bool DbSettings::getBool(const QString& name) {
return getSettings(name) == "TRUE";
}
void DbSettings::setBool(const QString& name, bool value) {
if (value)
setSettings(name, "TRUE");
else
setSettings(name, "FALSE");
}
QString DbSettings::getSettings(const QString& name) {
initTableSettings();
QSqlQuery query;
query.prepare("select value from SETTINGS where name=?");
query.addBindValue(name);
DbTools::execWithCheck(query);
if (query.next())
return query.value(0).toString();
return {};
}
void DbSettings::setSettings(const QString& name, const QString& value) {
initTableSettings();
QSqlQuery query;
query.prepare(
"replace into SETTINGS (name,value) values(?,?)");
query.addBindValue(name);
query.addBindValue(value);
DbTools::execWithCheck(query);
}