-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmaindatabase.cpp
More file actions
102 lines (89 loc) · 2.88 KB
/
maindatabase.cpp
File metadata and controls
102 lines (89 loc) · 2.88 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "maindatabase.h"
MainDataBase::MainDataBase(QString name)
{
openDataBase(name);
}
User MainDataBase::getUser(QString name)
{
if (db.isOpen())
{
QSqlQuery query;
QString strQuery = QString("SELECT * FROM ScrabbleDataBase WHERE name ='%1'").arg(name);
bool result = query.exec(strQuery);
QSqlRecord record = query.record();
assert(result);
while (query.next())
{
int loseCount = query.value(record.indexOf("loseCount")).toInt();
int winCount = query.value(record.indexOf("winCount")).toInt();
int usersCS = query.value(record.indexOf("usersCurrentScore")).toInt();
int botsCS = query.value(record.indexOf("botsCurrentScore")).toInt();
QString board = query.value(record.indexOf("currentBoard")).toString();
string sBoard = "";
for (int i = 0; i < (int)board.size(); ++i)
sBoard += board[i].toLatin1();
return User(name, loseCount, winCount, sBoard, usersCS, botsCS);
}
addUser(name);
return getUser(name);
}
}
vector<QString> MainDataBase::getAllUsers()
{
vector <QString> results;
results.clear();
if (db.isOpen())
{
QSqlQuery query;
QString strQuery = QString("SELECT * FROM ScrabbleDataBase");
bool result = query.exec(strQuery);
QSqlRecord record = query.record();
assert(result);
while (query.next())
{
QString name = query.value(record.indexOf("name")).toString();
results.push_back(name);
}
}
return results;
}
MainDataBase::~MainDataBase()
{
db.close();
}
void MainDataBase::addUser(QString name)
{
if (db.isOpen())
{
QSqlQuery query;
bool result = query.exec(QString("INSERT INTO ScrabbleDataBase(name, loseCount, winCount, currentBoard"
", usersCurrentScore, botsCurrentScore)"
"values('%1', '%2', '%3', '%4', '%5', '%6')").arg(name, 0, 0, "", 0, 0));
assert(result);
}
}
void MainDataBase::openDataBase(QString name)
{
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(name);
if (!db.isOpen())
{
if (!db.open())
{
assert(false);
}
}
if (!db.tables().contains("ScrabbleDataBase"))
{
QSqlQuery query;
QString strQuery = "CREATE table ScrabbleDataBase ("
"name VARCHAR(100) PRIMARY KEY,"
"loseCount integer,"
"winCount integer,"
"currentBoard VARCHAR(100),"
"usersCurrentScore integer,"
"botsCurrentScore integer);";
bool result = query.exec(strQuery);
assert(result);
}
}