-
Notifications
You must be signed in to change notification settings - Fork 0
/
databasemanager.cpp
143 lines (119 loc) · 3.91 KB
/
databasemanager.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
#include "databasemanager.h"
#include <QDebug>
#include <QPointF>
DatabaseManager::DatabaseManager(QObject *parent) :
QObject(parent)
{
}
bool DatabaseManager::openDB()
{
// Find QSLite driver
db = QSqlDatabase::addDatabase("QSQLITE");
#ifdef Q_OS_LINUX
// NOTE: We have to store database file into user home folder in Linux
QString path(QDir::home().path());
path.append(QDir::separator()).append("my.db.sqlite");
path = QDir::toNativeSeparators(path);
db.setDatabaseName(path);
#else
// NOTE: File exists in the application private folder, in Symbian Qt implementation
db.setDatabaseName("HistoryData.db");
#endif
// Open databasee
return db.open();
}
QSqlError DatabaseManager::lastError()
{
// If opening database has failed user can ask
// error description by QSqlError::text()
return db.lastError();
}
bool DatabaseManager::deleteDB()
{
// Close database
db.close();
#ifdef Q_OS_LINUX
// NOTE: We have to store database file into user home folder in Linux
QString path(QDir::home().path());
path.append(QDir::separator()).append("my.db.sqlite");
path = QDir::toNativeSeparators(path);
return QFile::remove(path);
#else
// Remove created database binary file
return QFile::remove("HistoryData.db");
#endif
}
bool DatabaseManager::createTable()
{
// Create table "HIS_INFO"
bool ret = false;
if (db.isOpen())
{
QSqlQuery query;
ret = query.exec("create table IF NOT EXISTS HIS_INFO(remoteip varchar, servername varchar, datetime varchar, memory varchar, cpu varchar)"); //新建HistoryInfo表
}
return ret;
}
int DatabaseManager::insertData(QString sRemoteIP, QString sServerName, QString sDateTime, double dMemory, double dCpu)
{
int newId = -1;
bool ret = false;
if (db.isOpen())
{
//http://www.sqlite.org/autoinc.html
// NULL = is the keyword for the autoincrement to generate next value
QSqlQuery query;
QString sSql = QString("insert into HIS_INFO values('%1','%2','%3',%4,%5)")
.arg(sRemoteIP).arg(sServerName).arg(sDateTime).arg(dMemory).arg(dCpu);
ret = query.exec(sSql);
// Get database given autoincrement value
if (ret)
{
// http://www.sqlite.org/c3ref/last_insert_rowid.html
newId = query.lastInsertId().toInt();
}
}
return newId;
}
bool DatabaseManager::getData(QString sServerType, QString sFromTime, QString sEndTime, HistoryData*& HIS_INFO)
{
bool ret = false;
QString sSql = QString("select * from HIS_INFO where servername = %1 and datetime >= %2 and datetime<= %2").arg(sServerType).arg(sFromTime).arg(sEndTime);
QSqlQuery query(sSql);
if (query.next())
{
HIS_INFO->sRemoteIP = query.value(0).toString();
HIS_INFO->sServerName = query.value(1).toString();
HIS_INFO->sDateTime = query.value(2).toString();
HIS_INFO->dMemory = query.value(3).toDouble();
HIS_INFO->dCPU = query.value(4).toDouble();
ret = true;
}
return ret;
}
bool DatabaseManager::getData(QString sServerType, QString sFromTime, QString sEndTime, QVector<QPointF> &CPU_data, QVector<QPointF> &MEM_data)
{
bool ret = false;
QString sSql = QString("select * from HIS_INFO where servername='%1' and datetime(datetime)>datetime('%2') and datetime(datetime)<datetime('%3')").arg(sServerType).arg(sFromTime).arg(sEndTime);
QSqlQuery query(sSql);
int i = -1;
while (query.next())
{
i++;
CPU_data.append(QPointF(i, query.value(3).toDouble()));
MEM_data.append(QPointF(i, query.value(4).toDouble()));
// ret = true;
}
ret = true;
return ret;
}
bool DatabaseManager::deleteData(int id)
{
bool ret = false;
if (db.isOpen())
{
QSqlQuery query;
ret = query.exec(QString("delete from HIS_INFO where id=%1").arg(id));
}
return ret;
}