当前位置:网站首页>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
边栏推荐
- [deep learning] handwritten neural network model preservation
- 信号与系统:希尔伯特变换
- 绘制轮廓 cv2.findContours函数及参数解释
- MySql与Qt连接、将数据输出到QT的窗口tableWidget详细过程。
- CRC-16 Modbus代码
- 学习率优化策略
- Positional argument after keyword argument
- Typora 安装包2021年11月最后一次免费版本的安装包下载V13.6.1
- [activiti] activiti environment configuration
- 顺序栈 C语言 进栈 出栈 遍历
猜你喜欢

【FatFs】手动移植FatFs,将SRAM虚拟U盘
![[MYCAT] MYCAT installation](/img/52/2f77ed64b2ed4e9297acaa8362e194.png)
[MYCAT] MYCAT installation

Answers and analysis of some after-school exercises in signals and systems (Wujing)

day2-WebSocket+排序

day-7 jvm完结
![[activiti] group task](/img/f1/b99cae9e840d3a91d0d823655748fe.png)
[activiti] group task

Statistical learning methods (2nd Edition) Li Hang Chapter 22 summary of unsupervised learning methods mind mapping notes

Machine learning (Zhou Zhihua) Chapter 3 Notes on learning linear models

《统计学习方法(第2版)》李航 第十三章 无监督学习概论 思维导图笔记

Qt新手入门级 计算器加、减、乘、除、应用
随机推荐
【USB Host】STM32H7 CubeMX移植带FreeRTOS的USB Host读取U盘,USBH_Process_OS卡死问题,有个值为0xA5A5A5A5
读取csv文件的满足条件的行并写入另一个csv中
[activiti] Introduction to activiti
西瓜书/南瓜书--第1,2章总结
《统计学习方法(第2版)》李航 第14章 聚类方法 思维导图笔记 及 课后习题答案(步骤详细) k-均值 层次聚类 第十四章
对ArrayList<ArrayList<Double>>排序
Erp+rpa opens up the enterprise information island, and the enterprise benefits are doubled
"Statistical learning methods (2nd Edition)" Li Hang Chapter 15 singular value decomposition SVD mind map notes and after-school exercise answers (detailed steps) SVD matrix singular value Chapter 15
[activiti] activiti environment configuration
++cnt1[s1.charAt(i) - ‘a‘];
Raspberry pie is of great use. Use the campus network to build a campus local website
STM32 DSP library MDK vc5\vc6 compilation error: 256, (const float64_t *) twiddlecoeff64_ 256, armBitRevIndexTableF64_ 256,
"Statistical learning methods (2nd Edition)" Li Hang Chapter 16 principal component analysis PCA mind map notes and after-school exercise answers (detailed steps) PCA matrix singular value Chapter 16
[MYCAT] MYCAT installation
AD1256
Add se channel attention module to the network
《统计学习方法(第2版)》李航 第十三章 无监督学习概论 思维导图笔记
[MYCAT] Introduction to MYCAT
Connect CRM system and effect advertising, help enterprises with precision marketing, and help enterprises with precision marketing
‘Results do not correspond to current coco set‘