-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.cpp
103 lines (97 loc) · 3.03 KB
/
database.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
/*
* Author: equati0n
* Date: December 2016
*/
//User Libraries
#include "database.h"
namespace DataB{
//*****************************************************************************
//*****************************************************************************
// Register User
//Inputs
// --> SQL Query
//Outputs
// <-- True/false-False on failure
///////////////////////////////////////////////////////////////////////////////
bool regUsr(Query Input,QSqlDatabase db){
//Check user input against the database
QString insert;
QSqlQuery Q(db);
if(chkUsr(Input,db)){
//Define SQL Query
insert="INSERT INTO mario_table (username,password) VALUES "
"('"+Input.uName+"', SHA1('"+Input.pass+"'))";
Q.exec(insert);
db.close();
return true;
//Upon Failure
}else{db.close(); return false;}
}
//*****************************************************************************
//*****************************************************************************
// Check Username
//Inputs
// --> SQL Query
//Outputs
// <-- True/false-False on failure
///////////////////////////////////////////////////////////////////////////////
bool chkUsr(Query Input,QSqlDatabase db){
QSqlQuery Q(db);
QString select;
//Setup a boolean
bool valid=true;
//Define the Query
select="SELECT username FROM mario_table WHERE username='"
+Input.uName+"'";
Q.exec(select);
if(Q.size()==1){ return false; }
//Upon Success
else{ return valid; }
}
//*****************************************************************************
//*****************************************************************************
// Coonects To Database
//Inputs
// --> Connection Structure
//Outputs
// <-- True/false-False on failure
///////////////////////////////////////////////////////////////////////////////
bool DBConnect(Connection C){
//Connect to database
C.db = QSqlDatabase::addDatabase("QMYSQL");
C.db.setHostName("");
C.db.setDatabaseName("");
C.db.setUserName("");
C.db.setPassword("");
//Check Connection
if(!C.db.open()){
C.connected=false;
return C.connected;
}
//Successful Connection
C.connected=true;
return C.connected;
}
//*****************************************************************************
//*****************************************************************************
// Check Username and Password
//Inputs
// --> SQL Query
//Outputs
// <-- True/false-False on failure
///////////////////////////////////////////////////////////////////////////////
bool cUsrPas(Query Input,QSqlDatabase db){
QSqlQuery Q(db);
QString select;
//Setup a boolean
bool valid=true;
//Define the Query
select="SELECT username FROM mario_table WHERE username='"
+Input.uName+"' AND password=SHA1('"+Input.pass+"')";
Q.exec(select);
//Check for a match
if(Q.size()!=1){ return false; }
//Upon Success
else{ return valid; }
}
}