-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabaseutil.cpp
83 lines (71 loc) · 2.25 KB
/
databaseutil.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
#include "databaseutil.h"
#include <QSettings>
#include <QSqlDatabase>
#include <QTextCodec>
#include <QDebug>
DataBaseUtil::DataBaseUtil()
{
this->loadDatabaseConfig();
}
bool DataBaseUtil::connectToDataBase()
{
bool isMysqlAvailable = QSqlDatabase::isDriverAvailable("QMYSQL");
if(!isMysqlAvailable)
{
qDebug() << "[Info] Available database driver: " << QSqlDatabase::drivers();
qDebug() << "[Error] QMYSQL Driver Not Available!";
return false;
}
QSqlDatabase databaseConnection =
QSqlDatabase::addDatabase("QMYSQL", "beyondgbrowse");
databaseConnection.setHostName(this->serverAddrress);
databaseConnection.setDatabaseName(this->databaseName);
databaseConnection.setUserName(this->username);
databaseConnection.setPassword(this->password);
bool isOpenDatabaseSuccess = databaseConnection.open();
if(isOpenDatabaseSuccess)
{
qDebug() << "[Info] Connect to database success!";
this->databaseConnection = databaseConnection;
}
else
{
qDebug() << "[Error] Connect to database fail!";
}
this->printDatabaseConfig();
return isOpenDatabaseSuccess;
}
QSqlDatabase DataBaseUtil::getDatabaseConnection()
{
return this->databaseConnection;
}
void DataBaseUtil::loadDatabaseConfig()
{
QSettings snowDbSettings("./config.ini", QSettings::IniFormat);
snowDbSettings.setIniCodec(QTextCodec::codecForName("UTF-8"));
snowDbSettings.beginGroup("Database");
this->serverAddrress = snowDbSettings.value(
QString("serverAddrress"),
QString("localhost")
).toString();
this->databaseName = snowDbSettings.value(
QString("databaseName"),
QString("beyondgbrowse")
).toString();
this->username = snowDbSettings.value(
QString("username"),
QString("snow_db201905")
).toString();
this->password = snowDbSettings.value(
QString("password"),
QString("snow_db201905")
).toString();
snowDbSettings.endGroup();
}
void DataBaseUtil::printDatabaseConfig()
{
qDebug() << "DatabaseUrl: " << this->serverAddrress;
qDebug() << "DatabaseName: " << this->databaseName;
qDebug() << "Username: " << this->username;
qDebug() << "Password: " << this->password;
}