-
Notifications
You must be signed in to change notification settings - Fork 0
/
databaseconnection.cpp
79 lines (69 loc) · 2.33 KB
/
databaseconnection.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
#include "databaseconnection.h"
void DatabaseConnection::setAuthentication(std::string url,std::string user, std::string password, std::string database)
{
if(m_connectionStatus)
{
signalMessage("MySQL connection error","#ERR: You need to close the existing connection...");
return;
}
m_url = url;
m_user = user;
m_password = password;
m_database = database;
openConnection();
}
void DatabaseConnection::openConnection() {
try {
m_con.reset(m_driver->connect(m_url,m_user,m_password));
m_con->setSchema(m_database);//this is the same as executeQuery("USE m_database");
m_stmt.reset(m_con->createStatement());
emit signalMessage("MySQL Database connection","Succesfully connected to MySQL Database!");
m_connectionStatus = 1;
}
catch(sql::SQLException &e) {
handleSqlException(e,"openConnection",__FILE__,__LINE__);
}
resetResults();
}
void DatabaseConnection::welcomeMessage() {
try {
m_res.reset(m_stmt->executeQuery("SELECT 'Welcome to MySQL/C++' AS _message"));
parseResults("_message");
}
catch(sql::SQLException &e) {
handleSqlException(e,"welcomeMessage",__FILE__,__LINE__);
}
resetResults();
}
void DatabaseConnection::showTables() {
try {
m_res.reset(m_stmt->executeQuery("SHOW TABLES"));
parseResults(1);
}
catch(sql::SQLException &e) {
handleSqlException(e,"showTables",__FILE__,__LINE__);
}
resetResults();
}
void DatabaseConnection::parseResults(int columnIndex) {
while(m_res->next())
qDebug() << m_res->getString(columnIndex).c_str();
}
void DatabaseConnection::parseResults(std::string columnName) {
while(m_res->next())
qDebug() << m_res->getString(columnName).c_str();
}
void DatabaseConnection::handleSqlException(sql::SQLException &e, std::string functionName, std::string fileName, int lineNumber) {
QString errorInfo = QString ("#ERR: SQLException in %1 (%2) on line %3\n#ERR: %4 (MySQL error code: %5, SQLState: %6)")
.arg(fileName.c_str())
.arg(functionName.c_str())
.arg(lineNumber)
.arg(e.what())
.arg(e.getErrorCode())
.arg(e.getSQLState().c_str());
emit signalMessage("MySQL Connection Error",errorInfo);
m_con.reset();
m_stmt.reset();
m_res.reset();
m_connectionStatus=0;
}