-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sql.cpp
87 lines (77 loc) · 2.01 KB
/
Sql.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
//
// Created by viking on 7/4/18.
//
#include <QtWidgets/QMessageBox>
#include <QApplication>
#include "Sql.h"
Sql::Sql(const QString hname, const QString dbname, const QString user, const QString pass ):
hostname(hname),databasename(dbname),username(user),password(pass)
{
connect();
}
Sql Sql::select(const QString &columns, const QString &table) {
sqlquery+=QString("SELECT ")+columns+" FROM "+table;
return *this;
};
Sql Sql::where(const QString & conditions) {
//check the Boolean expression statement for consistency
auto const leftparacount=conditions.count("(");
auto const rightparacount=conditions.count(")");
if (leftparacount!=rightparacount) std::cout<<"ERROR! parethesis don't match";
sqlquery+=QString(" WHERE ")+conditions;
return *this;
};
void Sql::connect()
{
db=QSqlDatabase::addDatabase("QMYSQL");
db.setHostName(hostname);
db.setDatabaseName(databasename);
db.setUserName(username);
db.setPassword(password);
bool ok=db.open();
//
if(!ok) {
QMessageBox mess;
mess.setText("ERROR,CANNOT open database");
mess.exec();
exit(1);
}
}
/*
Sql Sql::lpar()
{
sqlquery=QString(" ( ")+sqlquery;
return *this;
}
Sql Sql::rpar()
{
sqlquery=sqlquery+QString(" ) ");
return *this;
}
Sql Sql::and_(const QString & condition)
{
sqlquery = sqlquery + QString(" AND ") + condition;
return *this;
}
Sql Sql::or_(const QString & condition)
{
sqlquery = sqlquery + QString(" OR ") + condition;
return *this;
}
Sql Sql::not_(const QString & condition)
{
sqlquery = sqlquery + QString(" NOT ") + condition;
return *this;
}
*/
const QString Sql::getsql() {return sqlquery;}
QSqlQuery Sql::qry(QString qq) {
QSqlQuery q;
if (!q.exec(qq)) {
QMessageBox mess;
mess.setText(q.lastError().text());
mess.exec();
exit(1);
}
return(q);
}