forked from ChengUU/KernoManage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseConnection.cpp
More file actions
47 lines (43 loc) · 1.31 KB
/
DatabaseConnection.cpp
File metadata and controls
47 lines (43 loc) · 1.31 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
44
45
46
47
#include "DatabaseConnection.h"
DatabaseConnection::DatabaseConnection()
{
DatabaseConnection("QMYSQL","127.0.0.1",3306,"test","root","sa123456");
}
DatabaseConnection::DatabaseConnection(const string DATABASE_NAME, string hostName, int port, string databaseName, string userName, string pwd)
{
this->DATABASE_NAME=QString::fromStdString(DATABASE_NAME);
this->hostName=QString::fromStdString(hostName);
this->port=port;
this->databaseName=QString::fromStdString(databaseName);
this->userName=QString::fromStdString(userName);
this->pwd=QString::fromStdString(pwd);
}
QSqlDatabase DatabaseConnection::getConnection()
{
QSqlDatabase dbSQL;
if(QSqlDatabase::contains("qt_sql_default_connection"))
dbSQL = QSqlDatabase::database("qt_sql_default_connection");
else
dbSQL = QSqlDatabase::addDatabase("QMYSQL");
dbSQL.setHostName("127.0.0.1");
dbSQL.setPort(3306);
dbSQL.setDatabaseName("test");
dbSQL.setUserName("root");
dbSQL.setPassword("sa123456");
if(
!dbSQL.open()
)
{
qDebug()<<"this,warning,failure";
}
else
{
qDebug()<<"this,ok,success";
}
return dbSQL;
}
bool DatabaseConnection::close(QSqlDatabase dbSQL)
{
if(dbSQL.isOpen()) dbSQL.close();
return dbSQL.isOpen();
}