当前位置:网站首页>Use QT to connect to MySQL and create table numbers, write data, and delete data
Use QT to connect to MySQL and create table numbers, write data, and delete data
2022-07-24 05:59:00 【Joey Boye o (* ^ ^ ^ *) o】
Preface
MySQL Importance
Linux As an operating system ,Apache or Nginx As Web The server ,MySQL As a database ,PHP/Perl/Python As a server-side script interpreter . Because these four software are free or open source software (FLOSS), So it doesn't cost a cent ( Apart from the labor cost ) You can build a stability 、 Free website system , Known in the industry as “LAMP“ or “LNMP” Combine .
One 、 Introduction to Qt And MySql The connection of
Basically ,Qt Same as X Window Upper Motif,Openwin,GTK Graphical interface library and Windows On the platform MFC,OWL,VCL,ATL It's the same kind of thing .
On this basis , We're doing it Ubuntu Embedded development , Better can choose MysQl The server .
Two 、 complete Qt And MySql The connection of services needs to meet
- install MySql And set up the environment
- install Qt And set the applicable and MySql Environment
- If Qt Report errors :QMYSQL driver not loaded
1、 The solution is to take MySql Of libmysql.dll Copy to the corresponding compiler
2、 Be careful :QT Selected Windows32 Bits should correspond to 32 Bit libmysql.dll.
3、 Be careful :QT Selected Windows64 Bits should correspond to 64 Bit libmysql.dll.
4、 Be careful : It needs to be in the engineering documents xxxx.pro Add QT += sql As shown in the figure below
5、 Be careful : choice C:\mysql-5.7.37-win32\lib Medium libmysql.dll copy to C:\Qt\Qt5.9.0\5.9\mingw53_32\bin in ( Remember that the corresponding is 32 A still 64 Bit Be sure to match )libmysql.dll.
Qt Source code mainwindow.h
The code is as follows ( Example ):
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QMainWindow>
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QDebug>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QGridLayout>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
public:
void myLayout();
private slots:
void button_1_clicked();
void button_2_clicked();
void button_3_clicked();
void button_4_clicked();
protected:
QLabel *Label_1;
QLabel *Label_2;
QLabel *Label_3;
QLabel *Label_4;
QLineEdit *Line_1;
QLineEdit *Line_2;
QLineEdit *Line_3;
QLineEdit *Line_4;
QPushButton *Button_1;
QPushButton *Button_2;
QPushButton *Button_3;
QPushButton *Button_4;
};
#endif // MAINWINDOW_H
Qt Source code main.cpp
The code is as follows ( Example ):
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
w.setFixedSize(300,200);
w.setWindowTitle("MySql Connection tool ");
w.myLayout();
return a.exec();
}
Qt Source code mainwindow.cpp
The code is as follows ( Example ):
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::myLayout()
{
QWidget *centerWindow = new QWidget;
this->setCentralWidget(centerWindow);
Button_1 = new QPushButton(" Connect MySql");
Button_2 = new QPushButton(" Add data ");
Button_3 = new QPushButton(" Update data ");
Button_4 = new QPushButton(" Delete data ");
Line_1 = new QLineEdit;
Line_2 = new QLineEdit;
Line_3 = new QLineEdit;
Line_4 = new QLineEdit;
Label_1 = new QLabel("id");
Label_2 = new QLabel("name");
Label_3 = new QLabel("age");
Label_4 = new QLabel("score");
Button_1->setMinimumHeight(50);
Button_2->setMinimumHeight(50);
Button_3->setMinimumHeight(50);
Button_4->setMinimumHeight(50);
connect(Button_1,SIGNAL(clicked(bool)),this,SLOT(button_1_clicked()));
connect(Button_2,SIGNAL(clicked(bool)),this,SLOT(button_2_clicked()));
connect(Button_3,SIGNAL(clicked(bool)),this,SLOT(button_3_clicked()));
connect(Button_4,SIGNAL(clicked(bool)),this,SLOT(button_4_clicked()));
QGridLayout *layout = new QGridLayout;
layout->addWidget(Line_1,0,1,1,3,Qt::Alignment());
layout->addWidget(Line_2,1,1,1,3,Qt::Alignment());
layout->addWidget(Line_3,2,1,1,3,Qt::Alignment());
layout->addWidget(Line_4,3,1,1,3,Qt::Alignment());
layout->addWidget(Button_1,4,0,Qt::Alignment());
layout->addWidget(Button_2,4,1,Qt::Alignment());
layout->addWidget(Button_3,4,2,Qt::Alignment());
layout->addWidget(Button_4,4,3,Qt::Alignment());
layout->addWidget(Label_1,0,0,Qt::Alignment());
layout->addWidget(Label_2,1,0,Qt::Alignment());
layout->addWidget(Label_3,2,0,Qt::Alignment());
layout->addWidget(Label_4,3,0,Qt::Alignment());
layout->setMargin(12);
centerWindow->setLayout(layout);
}
void MainWindow::button_1_clicked()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("127.0.0.1");// database server ip
db.setUserName("root");// Database user name
db.setPassword("123456");// Database password
db.setDatabaseName("mydata");// Which database to use
// Use the default port number 3306 Well , Because locally
if( !db.open() )// Open database , If you return false Open failed
{
QMessageBox::warning(this," error ",db.lastError().text() );// Open the failure , Display error message
return;
}else
{
QMessageBox::warning(this," Congratulations ~~~","Mysql Successful connection !");
}
QSqlQuery query;
QString sql = "create table student(id int primary key auto_increment,name varchar(255),age int,score int);";
query.exec(sql);
}
void MainWindow::button_2_clicked()
{
int id = Line_1->text().toInt();
QString name = Line_2->text();
int age = Line_3->text().toInt();
int score = Line_4->text().toInt();
QString sql = QString("insert into student(id,name,age,score) values('%1','%2','%3','%4')").arg(id).arg(name).arg(age).arg(score);
QSqlQuery query;
query.exec(sql);
}
void MainWindow::button_3_clicked()
{
int id = Line_1->text().toInt();
QString name = Line_2->text();
int age = Line_3->text().toInt();
int score = Line_4->text().toInt();
QString sql = QString("update student set id='%1',name='%2',age='%3',score='%4' where id='%5'").arg(id).arg(name).arg(age).arg(score).arg(id);
QSqlQuery query;
query.exec(sql);
}
void MainWindow::button_4_clicked()
{
int id = Line_1->text().toInt();
QString sql = QString("delete from student where id = '%1'").arg(id);
QSqlQuery query;
query.exec(sql);
}
Effect real picture

Operation method
- Copy the code into the project .
- Download the project package and import it directly .
Project package free download link
The method of importing packages is shown in the following figure



MySql Environment settings and verification tools are added 、 Delete data function .
MySql Environment setting and qt Tool validation diagram link
边栏推荐
- PLSQL query data garbled
- 学习率优化策略
- Erp+rpa opens up the enterprise information island, and the enterprise benefits are doubled
- Native JS magnifying glass effect
- opencv读取avi视频报错:number < max_number in function ‘icvExtractPattern
- 【USB Host】STM32H7 CubeMX移植带FreeRTOS的USB Host读取U盘,USBH_Process_OS卡死问题,有个值为0xA5A5A5A5
- day5-jvm
- [MYCAT] related concepts of MYCAT
- LSTM神经网络
- Test whether the label and data set correspond after data enhancement
猜你喜欢
随机推荐
Chapter IV decision tree summary
Xshell远程访问工具
Points for attention in adding spp module to the network
Jupyter notebook选择conda环境
Sqlserver completely deleted
找ArrayList<ArrayList<Double>>中出现次数最多的ArrayList<Double>
Answers and analysis of some after-school exercises in signals and systems (Wujing)
tensorflow和pytorch框架的安装以及cuda踩坑记录
Machine learning (Zhou Zhihua) Chapter 4 notes on learning experience of decision tree
day1-jvm+leetcode
【深度学习】手把手教你写“手写数字识别神经网络“,不使用任何框架,纯Numpy
Qt char型转QString型 16进制与char型 转 16进制整型
[activiti] personal task
KMP代码分布详解
Chapter 5 neural network
如何解决训练集和测试集的分布差距过大问题
《统计学习方法(第2版)》李航 第十三章 无监督学习概论 思维导图笔记
PLSQL query data garbled
systemctl + journalctl
json.dumps()函数解析


![OSError: [WinError 127] 找不到指定的程序。Error loading “caffe2_detectron_ops.dll“ or one of its dependencies](/img/1d/4c9924c20f697011f0e9cda6616c12.png)






