-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbinterface.cpp
92 lines (74 loc) · 1.99 KB
/
dbinterface.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
#include "dbinterface.h"
DBInterface::DBInterface()
{
db = QSqlDatabase::addDatabase("QSQLITE");//set type of DB
db.setDatabaseName("newmydb.sqlite"); // connect to my DB
UpdateDB();
}
bool DBInterface::addData(const int code, const QString name, const QString description, const int quantity, const QString pathImg)
{
bool result =false;
db.open();
QSqlQuery q ;
q.prepare(QString("INSERT INTO items VALUES(?,?,?,?,?)"));
q.bindValue(0,code);
q.bindValue(1,name);
q.bindValue(2,description);
q.bindValue(3,quantity);
q.bindValue(4,pathImg);
if( q.exec()){
q.clear();
result = true;
}
q.clear();
db.close();
return result;
}
bool DBInterface::addData(const Data MyData)
{
return addData(MyData.code,MyData.name,MyData.description,MyData.quantity,MyData.pathImg);
}
const QVector<Data> * DBInterface::getAllData()
{
return this->AllData;
}
void DBInterface::UpdateDB()
{
if(AllData != nullptr)
delete AllData ;
AllData = new QVector<Data>;
db.open();
QSqlQuery q;
size_t i =0;
q.exec("SELECT * FROM items");
while(q.next()){
AllData->push_front( Data());
AllData[i].data()->code = q.value(0).toInt();
AllData[i].data()->name = q.value(1).toString();
AllData[i].data()->description = q.value(2).toString();
AllData[i].data()->quantity = q.value(3).toInt();
AllData[i].data()->pathImg = q.value(4).toString();
}
q.clear();
db.close();
}
void DBInterface::UpdateRow(Data * change)
{
db.open();
QSqlQuery q;
q.prepare("UPDATE items SET name = ?, descriere = ?,cantitate = ?,i_path = ? WHERE id = ?");
q.bindValue(0,change->name);
q.bindValue(1,change->description);
q.bindValue(2,change->quantity);
q.bindValue(3,change->pathImg);
q.bindValue(4,change->code);
q.exec();
q.clear();
db.close();
}
void DBInterface::DeleteRow(int idRow)
{
}
DBInterface::~DBInterface(){
delete AllData;
}