-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.cpp
More file actions
76 lines (64 loc) · 2.21 KB
/
database.cpp
File metadata and controls
76 lines (64 loc) · 2.21 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
#include "database.h"
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QProcess>
#include <itemtype.h>
DataBase::DataBase()
{
/// Подключает драйвер SQLite
/// Проверяет доступность БД
QSqlDatabase sdb = QSqlDatabase::addDatabase("QSQLITE");
QFile dbFile("database.sqlite");
sdb.setDatabaseName(dbFile.fileName());
if (!sdb.open()) {
qDebug() << sdb.lastError().text();
return;
}
createTables();
}
void DataBase::createTables()
{
/// Создает таблицы Инвентаря и Предмета
QSqlQuery query;
query.exec("CREATE TABLE Inventory "
"(`CellPosition` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, "
"`ItemCount` INTEGER NOT NULL DEFAULT 0, "
"`ItemType` INTEGER DEFAULT 0)");
query.exec("CREATE TABLE Item "
"(`Id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, "
"`Image` TEXT, "
"`Name` TEXT)");
int count = 0;
foreach (QString path, imagesPath){
query.prepare("INSERT INTO Item (Id, Image, Name) "
"VALUES (:id, :image, :name)");
query.bindValue(":id", count);
query.bindValue(":image", path);
query.bindValue(":name", itemsName.at(count));
query.exec();
count++;
}
}
void DataBase::insertItem(int id, int type)
{
/// Вставляет значение в таблицу Инвентаря
QSqlQuery query;
query.prepare("INSERT INTO Inventory (CellPosition, ItemType) "
"VALUES (:id, :type)");
query.bindValue(":id", id);
query.bindValue(":type", type);
query.exec();
}
void DataBase::updateItem(int id, int type, int count)
{
/// Обновляет значение таблицы Инвентаря
QSqlQuery query;
query.prepare("UPDATE Inventory "
"SET ItemType = :type, ItemCount = :count "
"WHERE CellPosition = :id");
query.bindValue(":id", id);
query.bindValue(":type", type);
query.bindValue(":count", count);
query.exec();
}