-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseatsdatabasecontroller.cpp
More file actions
99 lines (87 loc) · 2.31 KB
/
seatsdatabasecontroller.cpp
File metadata and controls
99 lines (87 loc) · 2.31 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
#include "seatsdatabasecontroller.h"
SeatsDatabaseController::SeatsDatabaseController(const QString &path)
{
m_seatsDatabase = QSqlDatabase::addDatabase("QSQLITE");
m_seatsDatabase.setDatabaseName(path);
if(!m_seatsDatabase.open())
{
qDebug() << "Error: connection with database failed";
}
else
{
qDebug() << "Database: connection ok ";
}
}
SeatsDatabaseController::~SeatsDatabaseController()
{
if(m_seatsDatabase.isOpen())
{
m_seatsDatabase.close();
}
}
bool SeatsDatabaseController::IsOpen()
{
return m_seatsDatabase.isOpen();
}
bool SeatsDatabaseController::CreatTable(const QString code)
{
bool success = false;
QSqlQuery query;
query.prepare("CREATE TABLE "+ code +"(id INTEGER PRIMARY KEY, is_checked INT(255));");
if (!query.exec())
{
qDebug() << "Couldn't create the table '" + code + "': one might already exist.";
success = false;
}
return success;
}
bool SeatsDatabaseController::AddSeat(int isChecked,const QString code)
{
bool success = false;
QSqlQuery queryAdd;
queryAdd.prepare("INSERT INTO " + code + " (is_checked) VALUES (:is_checked)");
queryAdd.bindValue(":is_checked",isChecked );
if(queryAdd.exec())
{
success = true;
qDebug() << "seat added";
}
else
{
qDebug() << "add "+ code +" failed" << queryAdd.lastError();
}
return success;
}
int SeatsDatabaseController::IsSeatChecked(int id, const QString code)
{
QSqlQuery queryGet;
queryGet.prepare("SELECT is_checked FROM " + code + " WHERE id = (:id)");
queryGet.bindValue(":id",id);
if(queryGet.exec())
{
while (queryGet.next())
{
return queryGet.value(0).toInt();
}
}
else
{
qDebug() << queryGet.lastError();
}
return 0;
}
void SeatsDatabaseController::updateSeatCheckedValue(int id, int isChecked, const QString code)
{
QSqlQuery query;
query.prepare("UPDATE " + code + " SET is_checked = :is_checked WHERE id = (:id)");
query.bindValue(":id",id);
query.bindValue(":is_checked",isChecked);
if (query.exec())
{
qDebug() << "updated";
}
else
{
qDebug() << "remove all trains failed: " << query.lastError();
}
}