-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySql.cpp
More file actions
85 lines (69 loc) · 1.89 KB
/
MySql.cpp
File metadata and controls
85 lines (69 loc) · 1.89 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
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
84
#include "MySql.h"
TopWidget::TopWidget(QWidget *parent,QString sqlSentence)
: QWidget(parent),m_sqlSentence(sqlSentence)
{
setSqlServerParameter("mine","root","nsznsz","localhost",3306);
this->sqlConnect();
//执行sql语句
m_query->exec(m_sqlSentence);
this->resize(420,400);
this->sqlShow();
}
TopWidget::~TopWidget()
{
m_db.close();
qDebug() << "数据库关闭";
}
void TopWidget::setSqlSentence(const QString &sqlSentence)
{
m_sqlSentence = sqlSentence;
}
//设置sql服务器参数
void TopWidget::setSqlServerParameter(const QString &DatabaseName,
const QString &UserName,
const QString &Password,
const QString &HostName,
const int &port)
{
m_DatabaseName = DatabaseName;
m_UserName = UserName;
m_Password = Password;
m_HostName = HostName;
m_port = port;
}
void TopWidget::sqlConnect()
{
//创建一个sqlDatabase
m_db = QSqlDatabase::addDatabase("QMYSQL");
/* 设置链接的相关属性 */
//设置database名称
m_db.setDatabaseName(m_DatabaseName);
//设置用户名
m_db.setUserName(m_UserName);
//设置密码
m_db.setPassword(m_Password);
//设置链接地址
m_db.setHostName(m_HostName);
//设置端口号
m_db.setPort(m_port);
//判断数据库是否打开
if(!m_db.open())
{
qDebug() << "数据库打开失败...";
QMessageBox::information(this,"注意","无法连接到数据库!<br>此功能暂时无法使用");
// return;
}
else
{
qDebug() << "数据库打开成功...";
m_query = new QSqlQuery(m_db);
}
}
void TopWidget::sqlShow()
{
m_model.setQuery(*m_query);
m_view.setParent(this);
m_view.setModel(&m_model);
m_view.resize(420,400);
m_view.show();
}