-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdbconn.cpp
More file actions
43 lines (39 loc) · 1.26 KB
/
Copy pathdbconn.cpp
File metadata and controls
43 lines (39 loc) · 1.26 KB
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
#include "dbconn.h"
#include <QMessageBox>
#include <QCoreApplication>
#include <QDir>
/**
* @author: Ethan
* @date: 2013-3-20
*/
DBConn::DBConn()
{
// QString fileName = ":/conn.xml";
qDebug() << QCoreApplication::applicationDirPath();
qDebug() << QDir::currentPath();
QString fileName = QCoreApplication::applicationDirPath() + "/config/dbconn.xml";
readConfig = new ReadConfig(fileName);
serverip = readConfig->getValue("serverip");
dbname = readConfig->getValue("dbname");
username = readConfig->getValue("username");
password = readConfig->getValue("password");
}
bool DBConn::createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName(serverip);
db.setDatabaseName(dbname);
db.setUserName(username);
db.setPassword(password);
if (!db.open()) {
const QString content = "Unable to establish a database connection.\n"
"Please make sure the server is up.\n"
"And check your host name: " + serverip;
QMessageBox::critical(0,
"Cannot open database",
content,
QMessageBox::Ok);
return false;
}
return true;
}