vs 2019
mysql server 8.0
下载下载科大镜像Qt 5.9.0
安装 跳过注册 安装路径安装路径不能带空格、中文字符或者其它任何特殊字符。
选择组件 使用 Qt designer设计UIdesigner的好处是可以拖拽设计UI,十分便捷,并且可以自动导出.h文件。
在 安装目录5.9mingw53_32bin 下找到designer.exe,打开。
Qt designer 使用教程
一
二
三
.ui转换为.huic *.ui -o *.hVisual Studio项目使用Qt
在扩展/管理扩展下载Qt Visual Studio Tools
关闭所有VS窗口进行下载
重启电脑生效
打开扩展/qt vs tools/qt versions 添加qt compiler。路径是 qt安装目录5.9msvc2017_64binqmake
创建新项目,搜索qt,选择qt widgets Application
将之前.ui生成的.h复制到项目中
vs连接mysql在项目属性c/c++常规中,添加附加包含目录: mysql安装目录MySQL Server 8.0include
在项目属性链接器常规中,添加附加包含目录: mysql安装目录MySQL Server 8.0lib
在项目属性链接器输入中,添加libmysql.lib
mysql安装目录MySQL Server 8.0lib中的libmysql.dll复制到 项目main.cpp所在目录中
登陆数据库,成功则显示登录成功,失败则提升原因
效果图
Database.h
#pragma once #include#include //#include "C:Program FilesMySQLMySQL Server 8.0includemysql.h" using std::cout; using std::endl; // 主要接口 // MYSQL //句柄 // MYSQL * mysql_init(MYSQL *mysql); //初始化一个MYSQL连接的实例对象,如果mysql不为空,返回该对象地址。 // void mysql_close(MYSQL *sock); //释放一个MYSQL连接实例 // MYSQL *mysql_real_connect ( // MYSQL *mysql, // const char *host, // const char *user, // const char *passwd, // const char *db, // unsigned int port, // const char *unix_socket, // unsigned long client_flag); // 连接请求 // int mysql_query(MYSQL *mysql, const char *stmt_str); //命令执行 返回1失败, 返回0成功 // MYSQL_RES *mysql_store_result(MYSQL *mysql); //储存结果集 // MYSQL_ROW mysql_fetch_row(MYSQL_RES *result); //检索一个结果集合的下一行。当在mysql_store_result()之后使用时,如果没有更多的行可检索时,mysql_fetch_row()返回NULL。 // unsigned int mysql_num_fields(MYSQL_RES *result); //函数返回结果集中字段的数。 // void mysql_free_result(MYSQL_RES *result); //释放结果集内存。 // const char* mysql_error(MYSQL* mysql); //返回上一个 MySQL 操作产生的文本错误信息。 class Database { public: Database() : _res(nullptr) { _mysql = new MYSQL; if (mysql_init(_mysql) == nullptr) { cout << "初始化失败" << endl; exit(0); } } ~Database() { if (_mysql != nullptr) { mysql_close(_mysql); delete _mysql; } if (_res != nullptr) { mysql_free_result(_res); } } //连接数据库 参数为host 用户名 密码 数据库名 端口 bool Connect(const char* host, const char* user, const char* passwd, const char* db, const int port); //执行命令 bool Query(const char* stmt_str); //MYSQL_ROW 等价于char** //返回结果的一行,知道返回nullptr MYSQL_ROW GetRow(); //返回上一个 MySQL 操作产生的文本错误信息。 const char* GetError() { return mysql_error(_mysql); } unsigned int GetFieldCount() { return _res == nullptr ? (unsigned int)0 : mysql_num_fields(_res); }; private: MYSQL* _mysql;//mysql连接 MYSQL_RES* _res; //执行结果 }; bool Database::Connect(const char* host, const char* user, const char* passwd, const char* db, const int port = 3306) { if (mysql_real_connect(_mysql, host, user, passwd, db, port, nullptr, 0) == nullptr) { cout << "连接数据库" << db << "失败" << endl; cout << GetError() << endl; return false; } else { cout << "连接数据库" << db << "成功" << endl; return true; } } bool Database::Query(const char* stmt_str) { if (mysql_query(_mysql, stmt_str) != 0) { cout << stmt_str << "执行失败" << endl; return false; } else { _res = mysql_store_result(_mysql); return true; } } MYSQL_ROW Database::GetRow() { return _res == nullptr ? nullptr : mysql_fetch_row(_res); }
UILogin.h
#pragma once #include#include #include #include #include #include #include QT_BEGIN_NAMESPACE class Ui_LoginDialog { public: QGridLayout* gridLayout; QLabel* label; QLineEdit* ipaddr; QLabel* label_2; QLineEdit* database; QLabel* label_3; QLineEdit* username; QLabel* label_4; QLineEdit* password; QPushButton* LoginBtn; void setupUi(QDialog* LoginDialog) { if (LoginDialog->objectName().isEmpty()) LoginDialog->setObjectName(QString::fromUtf8("LoginDialog")); LoginDialog->resize(400, 240); QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); sizePolicy.setHorizontalStretch(0); sizePolicy.setVerticalStretch(0); sizePolicy.setHeightForWidth(LoginDialog->sizePolicy().hasHeightForWidth()); LoginDialog->setSizePolicy(sizePolicy); LoginDialog->setMaximumSize(QSize(400, 240)); gridLayout = new QGridLayout(LoginDialog); gridLayout->setObjectName(QString::fromUtf8("gridLayout")); label = new QLabel(LoginDialog); label->setObjectName(QString::fromUtf8("label")); QFont font; font.setPointSize(12); label->setFont(font); gridLayout->addWidget(label, 0, 0, 1, 1); ipaddr = new QLineEdit(LoginDialog); ipaddr->setObjectName(QString::fromUtf8("ipaddr")); ipaddr->setFont(font); gridLayout->addWidget(ipaddr, 0, 1, 1, 1); label_2 = new QLabel(LoginDialog); label_2->setObjectName(QString::fromUtf8("label_2")); label_2->setFont(font); gridLayout->addWidget(label_2, 1, 0, 1, 1); database = new QLineEdit(LoginDialog); database->setObjectName(QString::fromUtf8("database")); database->setFont(font); gridLayout->addWidget(database, 1, 1, 1, 1); label_3 = new QLabel(LoginDialog); label_3->setObjectName(QString::fromUtf8("label_3")); label_3->setFont(font); gridLayout->addWidget(label_3, 2, 0, 1, 1); username = new QLineEdit(LoginDialog); username->setObjectName(QString::fromUtf8("username")); username->setFont(font); gridLayout->addWidget(username, 2, 1, 1, 1); label_4 = new QLabel(LoginDialog); label_4->setObjectName(QString::fromUtf8("label_4")); label_4->setFont(font); gridLayout->addWidget(label_4, 3, 0, 1, 1); password = new QLineEdit(LoginDialog); password->setObjectName(QString::fromUtf8("password")); password->setEchoMode(QLineEdit::Password); password->setFont(font); gridLayout->addWidget(password, 3, 1, 1, 1); LoginBtn = new QPushButton(LoginDialog); LoginBtn->setObjectName(QString::fromUtf8("LoginBtn")); LoginBtn->setFont(font); LoginBtn->setAutoFillBackground(true); gridLayout->addWidget(LoginBtn, 4, 1, 1, 1); retranslateUi(LoginDialog); QMetaObject::connectSlotsByName(LoginDialog); } // setupUi void retranslateUi(QDialog* LoginDialog) { LoginDialog->setWindowTitle(QCoreApplication::translate("LoginDialog", "LOGIN IN", nullptr)); label->setText(QCoreApplication::translate("LoginDialog", "Server Address:", nullptr)); label_2->setText(QCoreApplication::translate("LoginDialog", "Database Name:", nullptr)); label_3->setText(QCoreApplication::translate("LoginDialog", "Username357274232", nullptr)); label_4->setText(QCoreApplication::translate("LoginDialog", "Password357274232", nullptr)); LoginBtn->setText(QCoreApplication::translate("LoginDialog", "Login", nullptr)); } // retranslateUi }; QT_END_NAMESPACE
Login.h
#pragma once #include#include "UILogin.h" #include "Database.h" class LoginDialog : public QDialog{ public: LoginDialog(QDialog* parent = nullptr) : QDialog(parent) { _ui = new Ui_LoginDialog; _ui->setupUi(this); QObject::connect(_ui->LoginBtn, &QPushButton::clicked, this, &LoginDialog::Login); this->show(); } ~LoginDialog() { delete _ui; } private slots: void Login() { Database* mysql = new Database; auto host = (_ui->ipaddr->text()).toStdString(); auto db = (_ui->database->text()).toStdString(); auto user = (_ui->username->text()).toStdString(); auto passwd = (_ui->password->text()).toStdString(); if (mysql->Connect(host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), 3306)) { QMessageBox::information(this, "Title", "success"); } else { QMessageBox::information(this, "Title", mysql->GetError()); } } private: Ui_LoginDialog* _ui; };
main.cpp
#include "QtMysql.h" #include "Login.h" #includeint main(int argc, char* argv[]) { QApplication a(argc, argv); LoginDialog w; a.exec(); return 0; }
) {
QMessageBox::information(this, “Title”, “success”);
} else {
QMessageBox::information(this, “Title”, mysql->GetError());
}
}
private:
Ui_LoginDialog* _ui;
};
main.cpp ```c++ #include "QtMysql.h" #include "Login.h" #includeint main(int argc, char* argv[]) { QApplication a(argc, argv); LoginDialog w; a.exec(); return 0; }



