-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbhandler.cpp
47 lines (39 loc) · 915 Bytes
/
dbhandler.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
#include "dbhandler.h"
DbHandler::DbHandler() {
database_.removeDatabase("qt_sql_default_connection");
database_ = QSqlDatabase::addDatabase("QPSQL");
}
void DbHandler::init(std::vector<QString>& dbparams ,int port)
{
username_ = dbparams[0];
password_ = dbparams[1];
hostname_ = dbparams[2];
dbname_ = dbparams[3];
port_ = port;
}
QString DbHandler::dbConnect()
{
if(database_.isOpen())
{
database_.close();
}
database_.setHostName(hostname_);
database_.setDatabaseName(dbname_);
database_.setPort(port_);
database_.open(username_,password_);
if(database_.isOpen())
return QString("Database connected");
else
return database_.lastError().text();
}
void DbHandler::close()
{
if(database_.isOpen())
{
database_.close();
}
}
QString DbHandler::lastError()
{
return database_.lastError().text();
}