-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataBase.cpp
More file actions
88 lines (70 loc) · 2.01 KB
/
DataBase.cpp
File metadata and controls
88 lines (70 loc) · 2.01 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "DataBase.h"
#include <QDebug>
#include <QSqlQuery>
#include <QSqlError>
#include <QBuffer>
#include <QSqlRecord>
constexpr char c_szDatabasePath[] = "screen-shots.db";
DataBase::DataBase()
{
}
void DataBase::Connect()
{
m_db = QSqlDatabase::addDatabase("QSQLITE");
m_db.setDatabaseName(c_szDatabasePath);
if (!m_db.open())
{
qDebug() << "Error: connection with database failed";
return;
}
auto tables = m_db.tables();
// add tables
if(tables.empty())
{
QSqlQuery query;
query.prepare(
"CREATE TABLE screenshots ( "
"record_id INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, "
"hash VARCHAR(128), "
"similarity FLOAT(3.2), "
"bitmap LONGBLOB"
");");
if(!query.exec())
{
emit Error("Failed to create table: " + query.lastError().text());
}
}
}
void DataBase::AddScreenShot(const ScreenShot& shot)
{
QSqlQuery query;
query.prepare("INSERT INTO screenshots (hash, similarity, bitmap) VALUES ("
":hash, :similarity, :bitmap"
");");
query.bindValue(":hash", shot.hash);
query.bindValue(":similarity", shot.similarity);
query.bindValue(":bitmap", shot.pngImage, QSql::In | QSql::Binary);
query.finish();
if(!query.exec())
{
emit Error("Failed to add new screenshot.");
}
}
QByteArray DataBase::GetLastCapturedImage()
{
QSqlQuery query;
query.prepare("SELECT bitmap, MAX(record_id) FROM screenshots GROUP BY record_id LIMIT 1;");
//query.prepare("SELECT * FROM screenshots");
query.finish();
if(!query.exec())
{
emit Error("Failed to exec select statement.");
return {};
}
while (query.next()) {
int fieldNo = query.record().indexOf("bitmap");
auto image = query.value(fieldNo);
return image.toByteArray();
}
return {};
}