-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectDB.cpp
132 lines (118 loc) · 3.99 KB
/
connectDB.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
#include "connectDB.h"
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QApplication>
connectDB::connectDB()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("path");
if (!db.open()) {
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection.\n\n"
"Click Cancel to exit."), QMessageBox::Cancel);
exit(1);
}
QSqlQuery dada;
query=dada;
query.exec("create table magazyn ("
"id int primary key,"
"PLU int, "
"quantiny int, "
"nazwa varchar(30), "
"grupa1 varchar(30), "
"grupa2 varchar(30), "
"miejsce varchar(20), "
"stan varchar(20), "
"datein1 date, "
"datein2 date, "
"datein3 date,"
"dateout1 date,"
"dateout2 date, "
"dateout3 date)");
}
void connectDB::add_product(int id,QString PLU, QString quantiny, QString name, QString group1,
QString group2, QString place, QString condition,QString datein1,
QString datein2, QString datein3, QString dateout1,
QString dateout2, QString dateout3){
QString i = QString::number(id);
query.exec("insert into magazyn values('"+i+"','"+PLU+"', '"+quantiny+"', '"+name+"', "
"'"+group1+"', '"+group2+"', '"+place+"', '"+condition+"', '"+datein1+"', '"+datein2+"', "
"'"+datein3+"', '"+dateout1+"', '"+dateout2+"', '"+dateout3+"')");
}
void connectDB::manipulate(int id,int add,int collect,QString date)
{
int ka;
QString date1,date2,date3;
if(add&&collect)
{
query.prepare("delete from magazyn where id=:ida;");
query.bindValue(":ida",id);
query.exec();
return;
}
query.prepare("select quantiny from magazyn where id=:ida"
"");
query.bindValue(":ida",id);
query.exec();
while (query.next()) ka=query.value(0).toInt();
if(add)
{
ka=ka+add;
query.prepare("select datein1, datein2, datein3 from magazyn where id=:ida"
"");
query.bindValue(":ida",id);
query.exec();
while (query.next())
{
date1=query.value(0).toString();
date2=query.value(1).toString();
date3=query.value(2).toString();
}
date3=date2;
date2=date1;
date1=date;
query.prepare("update magazyn "
"set quantiny=:q,datein1=:d1, datein2=:d2, datein3=:d3 "
"where id=:ida ;");
query.bindValue(":q",ka);
query.bindValue(":d1",date1);
query.bindValue(":d2",date2);
query.bindValue(":d3",date3);
query.bindValue(":ida",id);
query.exec();
}
else
{
if (collect>ka) {
QMessageBox::critical(0, qApp->QObject::tr("Error"),
qApp->QObject::tr("Too little \n"
"Try with fewer \n"), QMessageBox::Cancel);
return;
}
ka=ka-collect;
query.prepare("select dateout1, dateout2, dateout3 from magazyn where id=:ida"
"");
query.bindValue(":ida",id);
query.exec();
while (query.next())
{
date1=query.value(0).toString();
date2=query.value(1).toString();
date3=query.value(2).toString();
}
date3=date2;
date2=date1;
date1=date;
query.prepare("update magazyn "
"set quantiny=:q,dateout1=:d1, dateout2=:d2, dateout3=:d3 "
"where id=:ida;");
query.bindValue(":q",ka);
query.bindValue(":d1",date1);
query.bindValue(":d2",date2);
query.bindValue(":d3",date3);
query.bindValue(":ida",id);
query.exec();
}
}