Qt——连接mysql增删查改(仓库管理极简版)

目录

UI布局设计

.pro文件

 mainwindow.h

main.cpp


UI布局设计

.pro文件

QT       += core gui
QT       += core gui sql
QT       += sqlgreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++11# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \mainwindow.cppHEADERS += \mainwindow.hFORMS += \mainwindow.ui# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

 mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QMessageBox>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QDebug>
#include <QtSql/QSqlQueryModel>QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private slots:void ReshowTable_goods();void ReshowTable_people();void on_move_goods_clicked();void on_move_people_clicked();void on_pushButton_addgoods_clicked();void on_pushButton_selectgoods_clicked();void on_pushButton_changegoods_clicked();void on_pushButton_deletegoods_clicked();void on_pushButton_addpeople_clicked();void on_pushButton_selectpeople_clicked();void on_pushButton_changepeople_clicked();void on_pushButton_deletepeople_clicked();private:Ui::MainWindow *ui;QSqlDatabase db= QSqlDatabase::addDatabase("QMYSQL");QSqlQueryModel *goodsMode;//货物数据模型QSqlQueryModel *peopleMode;//工作人员数据模型};
#endif // MAINWINDOW_H

main.cpp

#include "mainwindow.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);MainWindow w;w.show();return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
void sqlconnect(QWidget* parent);
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);goodsMode = new QSqlQueryModel(ui->tableView_goods);peopleMode = new QSqlQueryModel(ui->tableView_people);//绑定sqlconnect(this);}void sqlconnect(QWidget* parent)
{//连接数据库QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");db.setHostName("127.0.0.1");db.setPort(3306);db.setDatabaseName("sqlconnect");//数据库名称db.setUserName("root");//用户名db.setPassword("");//密码qDebug()<<"available drivers:";QStringList drivers = QSqlDatabase::drivers();for(auto driver: drivers)qDebug() << driver;bool ok = db.open();if(ok==0){QMessageBox::information(parent, "提示","数据库连接失败");parent->close();qDebug()<<"数据库连接失败";}//若数据库中没有表,则新建QSqlQuery query;QString createTableGoods="create table if not exists goods(id int primary key auto_increment,name varchar(10),number int, price varchar(10), data varchar(15),server varchar(10),endline int,code varchar(15));";QString createTableWoker="create table if not exists woker(name varchar(10),phonenumber varchar(20), sex varchar(8), age int);";query.exec(createTableGoods);query.exec(createTableWoker);
}MainWindow::~MainWindow()
{delete ui;
}
//___________________________________________________________________________________________________________
void MainWindow::ReshowTable_goods()
{goodsMode->setHeaderData(0,Qt::Horizontal,tr("入库编号"));goodsMode->setHeaderData(1,Qt::Horizontal,tr("名称"));goodsMode->setHeaderData(2,Qt::Horizontal,tr("数量"));goodsMode->setHeaderData(3,Qt::Horizontal,tr("单价"));goodsMode->setHeaderData(4,Qt::Horizontal,tr("生产日期"));goodsMode->setHeaderData(5,Qt::Horizontal,tr("供应商"));goodsMode->setHeaderData(6,Qt::Horizontal,tr("保质期"));goodsMode->setHeaderData(7,Qt::Horizontal,tr("条形码编号"));ui->tableView_goods->setModel(goodsMode);
}void MainWindow::on_move_goods_clicked()
{ui->stackedWidget->setCurrentIndex(0);
}void MainWindow::on_move_people_clicked()
{ui->stackedWidget->setCurrentIndex(1);
}void MainWindow::on_pushButton_addgoods_clicked()
{QString id = ui->lineEdit_goods_id->text();QString name = ui->lineEdit_goods_name->text();QString number = ui->lineEdit_goods_number->text();QString price = ui->lineEdit_goods_price->text();QString data = ui->lineEdit_goods_data->text();QString server = ui->lineEdit_goods_server->text();QString endline = ui->lineEdit_goods_endline->text();QString code = ui->lineEdit_goods_code->text();QSqlQuery query;QString str1 = "" + id +",'"+ name +"'," + number + ",'" +price+"','"+data+"','" +server+"',"+endline+",'"+ code+"'";QString str = "insert into goods(id,name,number,price,data,server,endline,code) values("+ str1 +");";bool success = query.exec(str);if(success){QMessageBox::information(this, "提示","入库成功");}else{QMessageBox::information(this, "提示","入库失败,请检查输入的数据是否正确");}goodsMode->setQuery("select * from goods;");ReshowTable_goods();
}void MainWindow::on_pushButton_selectgoods_clicked()
{goodsMode->setQuery("select * from goods;");ReshowTable_goods();
}void MainWindow::on_pushButton_changegoods_clicked()
{QSqlQuery query;QString str = "update goods set id = '"+ ui->lineEdit_goods_id->text() + "', name='" + ui->lineEdit_goods_name->text()+ "',number='" + ui->lineEdit_goods_number->text() + "',price='" + ui->lineEdit_goods_price->text()+ "',server='" + ui->lineEdit_goods_server->text()+ "',data='" + ui->lineEdit_goods_data->text()+ "',code='" + ui->lineEdit_goods_code->text()+ "',endline='" + ui->lineEdit_goods_endline->text()+ "' where number='" + ui->lineEdit_goods_id->text() + "';";query.exec(str);QMessageBox::information(this, "修改成功", "表信息修改成功");goodsMode->setQuery("select * from goods;");ReshowTable_goods();
}void MainWindow::on_pushButton_deletegoods_clicked()
{QSqlQuery query;int currentrow = ui->tableView_goods->currentIndex().row();goodsMode->removeRow(currentrow);if(currentrow != -1)//若选中一行{int ok  =  QMessageBox::warning(this,tr("删除当前行!"),tr("你确定删除当前行吗?"), QMessageBox::Yes,QMessageBox::No);//如确认删除if(ok == QMessageBox::Yes){goodsMode->removeRow(currentrow);QString id = goodsMode->data(goodsMode->index(currentrow,0)).toString();//获取idQString del = "delete from goods where id='" + id + "';";query.exec(del);goodsMode->setQuery("select * from goods");ui->tableView_goods->setModel(goodsMode);QMessageBox::information(this, "删除成功", "所选信息删除成功");}}elseQMessageBox::information(this, "提示", "请选择你要删除的信息行");
}//__________________________________________________________________________________________________________________
void MainWindow::ReshowTable_people()
{peopleMode->setHeaderData(0,Qt::Horizontal,tr("姓名"));peopleMode->setHeaderData(1,Qt::Horizontal,tr("性别"));peopleMode->setHeaderData(2,Qt::Horizontal,tr("年龄"));peopleMode->setHeaderData(3,Qt::Horizontal,tr("电话"));ui->tableView_people->setModel(peopleMode);}
void MainWindow::on_pushButton_addpeople_clicked()
{QString name = ui->lineEdit_people_name->text();QString sex = ui->lineEdit_people_sex->text();QString age = ui->lineEdit_people_age->text();QString phonenumber = ui->lineEdit_people_phonenumber->text();QSqlQuery query;QString str1 = "'" + name +"','"+ phonenumber +"','" + sex + "'," +age+"";QString str = "insert into woker(name,phonenumber,sex,age) values("+ str1 +");";bool success = query.exec(str);if(success){QMessageBox::information(this, "提示","增加成功");}else{QMessageBox::information(this, "提示","操作失败,请检查输入的数据是否正确");}goodsMode->setQuery("select * from woker;");ReshowTable_people();
}void MainWindow::on_pushButton_selectpeople_clicked()
{peopleMode->setQuery("select * from woker;");ReshowTable_people();}void MainWindow::on_pushButton_changepeople_clicked()
{QSqlQuery query;QString str = "update goods set name = '"+ ui->lineEdit_people_name->text() + "', sex='" + ui->lineEdit_people_sex->text()+ "',age=" + ui->lineEdit_people_age->text() + ",phonenumber='" + ui->lineEdit_people_phonenumber->text()+ "' where number='" + ui->lineEdit_goods_id->text() + "';";query.exec(str);QMessageBox::information(this, "修改成功", "表信息修改成功");goodsMode->setQuery("select * from woker;");ReshowTable_people();
}void MainWindow::on_pushButton_deletepeople_clicked()
{QSqlQuery query;int currentrow = ui->tableView_people->currentIndex().row();peopleMode->removeRow(currentrow);if(currentrow != -1)//若选中一行{int ok  =  QMessageBox::warning(this,tr("删除当前行!"),tr("你确定删除当前行吗?"), QMessageBox::Yes,QMessageBox::No);//如确认删除if(ok == QMessageBox::Yes){peopleMode->removeRow(currentrow);QString name = peopleMode->data(peopleMode->index(currentrow,0)).toString();//获取姓名QString del = "delete from woker where name='" + name + "';";query.exec(del);peopleMode->setQuery("select * from woker");ui->tableView_people->setModel(peopleMode);QMessageBox::information(this, "删除成功", "所选信息删除成功");}}elseQMessageBox::information(this, "提示", "请选择你要删除的信息行");
}

连接本地mysql,如果要在别的地方运行,要有mysql的环境

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/169150.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MySQL的索引和复合索引

由于MySQL自动将主键加入到二级索引&#xff08;自行建立的index&#xff09;里&#xff0c;所以当select的是主键或二级索引就会很快&#xff0c;select *就会慢。因为有些列是没在索引里的 假设CA有1kw人咋整&#xff0c;那我这个索引只起了前一半作用。 所以用复合索引&am…

腾讯云3年期轻量应用服务器优惠(薅羊毛教程)

腾讯云轻量应用服务器特价是有新用户限制的&#xff0c;所以阿腾云建议大家选择3年期轻量应用服务器&#xff0c;一劳永逸&#xff0c;免去续费困扰。腾讯云轻量应用服务器3年优惠可以选择2核2G4M和2核4G5M带宽&#xff0c;3年轻量2核2G4M服务器540元&#xff0c;2核4G5M轻量应…

2023年A股借壳上市研究报告

第一章 借壳上市概况 1.1 定义 借壳上市作为一种独特的资本市场操作手法&#xff0c;历来是企业拓展融资渠道和实现市场战略目标的重要途径。具体来说&#xff0c;借壳上市可分为狭义与广义两种模式。在狭义的定义下&#xff0c;借壳上市是指一家已上市的公司的控股母公司&am…

YOLO目标检测——水果检测数据集下载分享【含对应voc、coco和yolo三种格式标签】

实际项目应用&#xff1a;水果分类检测数据集的应用场景主要包括农贸市场监管、水果品质检测、超市零售管理等数据集说明&#xff1a;水果分类检测数据集&#xff0c;真实场景的高质量图片数据&#xff0c;数据场景丰富&#xff0c;含有苹果香蕉橙子图片标签说明&#xff1a;使…

Leetcode—2471.逐层排序二叉树所需的最少操作数目【中等】(置换环解法!)

2023每日刷题&#xff08;二十七&#xff09; Leetcode—2471.逐层排序二叉树所需的最少操作数目 置换环解题思想 参考自网络 总交换次数 每一层最小交换次数之和 每一层元素个数 - 置换环数 实现代码 /*** Definition for a binary tree node.* struct TreeNode {* …

公开数据集:灵长类动物多通道感觉运动皮层电生理学的研究

Nonhuman Primate Reaching with Multichannel Sensorimotor Cortex Electrophysiology. 1 公开数据集网址&#xff1a;https://zenodo.org/records/3854034 目录 General DescriptionPossible usesVariable namesDecoder ResultsVideosSupplementsContact InformationCitation…

[云原生案例2.4 ] Kubernetes的部署安装 【通过Kubeadm部署Kubernetes高可用集群】

文章目录 1. 基本架构及前置准备1.1 基本架构1.2 前置准备 2. 系统初始化操作 ---- 所有节点2.1 关闭防火墙、selinux和swap分区2.1.1 关闭防火墙和selinux2.1.2 关闭交换分区 2.2 修改主机名&#xff0c;添加域名映射2.2.1 修改主机名2.2.2 修改本地hosts文件 2.3 内核升级2.4…

Spark的转换算子和操作算子

1 Transformation转换算子 1.1 Value类型 1&#xff09;创建包名&#xff1a;com.shangjack.value 1.1.1 map()映射 参数f是一个函数可以写作匿名子类&#xff0c;它可以接收一个参数。当某个RDD执行map方法时&#xff0c;会遍历该RDD中的每一个数据项&#xff0c;并依次应用f函…

k8s 部署mqtt —— 筑梦之路

mqtt是干嘛的&#xff0c;网上有很多资料&#xff0c;这里就不再赘述。 --- apiVersion: apps/v1 kind: Deployment metadata:labels:app: mqttname: mqttnamespace: default spec:replicas: 1selector:matchLabels:app: mqttstrategy:rollingUpdate:maxSurge: 25%maxUnavaila…

神经网络(第二周)

一、简介 1.1 需求预测示例 1.1.1 逻辑回归算法 根据价格预测商品是否畅销。特征&#xff1a;T恤的价格&#xff1b;分类&#xff1a;销售量高1/销售量低0&#xff1b;使用逻辑回归算法进行分类&#xff0c;拟合效果如下图所示&#xff1a; 1.1.2 神经元和神经网络 将逻辑回…

Python开源项目PGDiff——人脸重建(Face Restoration),模糊清晰、划痕修复及黑白上色的实践

python ansconda 等的下载、安装等请参阅&#xff1a; Python开源项目CodeFormer——人脸重建&#xff08;Face Restoration&#xff09;&#xff0c;模糊清晰、划痕修复及黑白上色的实践https://blog.csdn.net/beijinghorn/article/details/134334021 友情提示&#xff1a; …

算法导论笔记5:贪心算法

P216 第15章动态规划 最优子结构 具有它可能意味着适合应用贪心策略 动态规划(Dynamic Programming)算法的核心思想是:将大问题划分为小问题进行解决,从而一步步获取最优解的处理算法。 剪切-粘贴技术证明 每个子问题的解就是它本身的最优解&#xff08;利用反证法&#xff0…