forked from anak10thn/ignsdk-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ignsql.cpp
70 lines (58 loc) · 1.71 KB
/
ignsql.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
#include "ignsql.h"
ignsql::ignsql(QObject *parent) :
QObject(parent)
{
}
bool ignsql::driver(const QString &drv, QString connect){
if(drv == "mysql"){
QStringList con = connect.split(":");
this->db = QSqlDatabase::addDatabase("QMYSQL");
this->db.setHostName(con.value(0));
this->db.setUserName(con.value(1));
this->db.setPassword(con.value(2));
this->db.setDatabaseName(con.value(3));
return this->db.open();
}
else if (drv == "sqlite2"){
this->db = QSqlDatabase::addDatabase("QSQLITE2");
this->db.setDatabaseName(connect);
return this->db.open();
}
else if (drv == "sqlite"){
this->db = QSqlDatabase::addDatabase("QSQLITE");
this->db.setDatabaseName(connect);
return this->db.open();
}
}
QString ignsql::query(const QString &qr){
QSqlQuery qry(this->db);
qry.prepare(qr);
qry.exec();
QSqlRecord data = qry.record();
QVariantList datarec;
QVariantMap map;
QVariantMap contentmap;
bool status;
while (qry.next()) {
for(int index = 0; index < data.count(); index++) {
QString key = data.fieldName(index);
QVariant value = qry.value(index);
map.insert(key, value);
}
datarec << map;
}
contentmap.insert("content",datarec);
if(qry.size() > 0){
status = true;
}
else {
status = false;
}
contentmap.insert("status",status);
//QJson::Serializer serializer;
//bool ok;
//QByteArray json = serializer.serialize(contentmap, &ok);
QJsonDocument json_enc = QJsonDocument::fromVariant(contentmap);
QByteArray json = json_enc.toJson();
return json;
}