通过数据库实现学生管理系统
widget.cpp
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//判断数据库对象是否包含了自己使用的数据库 Studemt.dbif(!db.contains("Student.db")){//添加一个数据库db = QSqlDatabase::addDatabase("QSQLITE"); //表示使用的是sqlite3版本的数据库//给数据库命名db.setDatabaseName("Student.db");}//打开数据库if(!db.open()){QMessageBox::information(this,"提示","数据库打开失败");return;}//此时说明数据库创建完毕并打开//就可以创建数据表了//需要使用SQL语句,需要使用QSQLQuerry类对象完成//准备SQL语句QString sql = "create table if not exists myTable(" //创建表的sql语句"id integer primary key autoincrement," //id主键,允许自增"numb integer," //学号,是整形"name varchar(10)," //姓名 字符串"score integer,""sex varchar(4))"; //性别 字符串//定义语句执行者QSqlQuery querry;//使用queery执行SQL语句if(!querry.exec(sql)){QMessageBox::information(this,"失败","创建表失败");}else{QMessageBox::information(this,"成功","创建表成功");}
}Widget::~Widget()
{delete ui;
}//录入按钮对应的槽函数
void Widget::on_btn1_clicked()
{//获取ui界面中要存入数据库中的数据int numb_ui = ui->edit1->text().toInt(); //获取学号QString name_ui = ui->edit2->text(); //获取姓名int score_ui = ui->edit3->text().toInt(); //获取分数QString sex_ui = ui->edit4->text(); //获取性别if(numb_ui == 0 || name_ui.isEmpty() || score_ui == 0||sex_ui.isEmpty()){QMessageBox::information(this,"提示","请将信息填充完整");return ;}//准备SQL语句QString sql = QString("insert into myTable(numb,name,score,sex)""values(%1,'%2',%3,'%4')").arg(numb_ui).arg(name_ui).arg(score_ui).arg(sex_ui);//定义语句执行官QSqlQuery querry;//调用执行者的相关函数执行SQL语句if(!querry.exec(sql)){QMessageBox::information(this,"失败","插入失败");}else{QMessageBox::information(this,"成功","插入成功");}
}//展示按钮对应的槽函数
void Widget::on_btn2_clicked()
{//准备SQL语句QString sql = "select * from myTable";//准备语句执行者QSqlQuery querry;if(!querry.exec(sql)){QMessageBox::information(this,"失败","查找失败");return;}//此时将查找的所有结果全都都放在querry对象中//可以通过next函数不断遍历查询结果int i = 0; //记录行号while(querry.next()){//遍历任意一组记录,querry.record//querry.record();//要找到每条一条记录 中的每个数据使用//qDebug() << querry.record().value(2);//返回当前记录对应数据项的个数//querry.record().count();//将记录的某一项的数据转变成了字符串//querry.record().value(2).toString();//将数据库中的表格展示到ui界面//ui->tableWidget->setItem();for(int j = 0; j<querry.record().count()-1; j++){ui->tableWidget->setItem(i,j,new QTableWidgetItem(querry.record().value(j+1).toString()));}i++; //进行下一行}
}//查找按钮
void Widget::on_btn3_clicked()
{//获取ui界面中要存入数据库中的数据int numb_ui = ui->edit1->text().toInt(); //获取学号QString name_ui = ui->edit2->text(); //获取姓名int score_ui = ui->edit3->text().toInt(); //获取分数QString sex_ui = ui->edit4->text(); //获取性别if(numb_ui != 0){ui->tableWidget->clear();//准备SQL语句QString sql = QString("select * from myTable where %1 == numb").arg(numb_ui);//准备语句执行者QSqlQuery querry;if(!querry.exec(sql)){QMessageBox::information(this,"失败","查找失败");return;}int i = 0; //记录行号while(querry.next()){for(int j = 0; j<querry.record().count()-1; j++){ui->tableWidget->setItem(i,j,new QTableWidgetItem(querry.record().value(j+1).toString()));}i++; //进行下一行}}else if(!name_ui.isEmpty()){ui->tableWidget->clear();//准备SQL语句QString sql = QString("select * from myTable where '%2' == name").arg(name_ui);//准备语句执行者QSqlQuery querry;if(!querry.exec(sql)){QMessageBox::information(this,"失败","查找失败");return;}int i = 0; //记录行号while(querry.next()){for(int j = 0; j<querry.record().count()-1; j++){ui->tableWidget->setItem(i,j,new QTableWidgetItem(querry.record().value(j+1).toString()));}i++; //进行下一行}}else if(score_ui != 0){ui->tableWidget->clear();//准备SQL语句QString sql = QString("select * from myTable where %3 == score").arg(score_ui);//准备语句执行者QSqlQuery querry;if(!querry.exec(sql)){QMessageBox::information(this,"失败","查找失败");return;}int i = 0; //记录行号while(querry.next()){for(int j = 0; j<querry.record().count()-1; j++){ui->tableWidget->setItem(i,j,new QTableWidgetItem(querry.record().value(j+1).toString()));}i++; //进行下一行}}else if(!sex_ui.isEmpty()){ui->tableWidget->clear();//准备SQL语句QString sql = QString("select * from myTable where '%4' == sex").arg(sex_ui);//准备语句执行者QSqlQuery querry;if(!querry.exec(sql)){QMessageBox::information(this,"失败","查找失败");return;}int i = 0; //记录行号while(querry.next()){for(int j = 0; j<querry.record().count()-1; j++){ui->tableWidget->setItem(i,j,new QTableWidgetItem(querry.record().value(j+1).toString()));}i++; //进行下一行}}
}//删除按钮
void Widget::on_btn4_clicked()
{//获取ui界面中要存入数据库中的数据int numb_ui = ui->edit1->text().toInt(); //获取学号QString name_ui = ui->edit2->text(); //获取姓名int score_ui = ui->edit3->text().toInt(); //获取分数QString sex_ui = ui->edit4->text(); //获取性别if(numb_ui != 0 ){ui->tableWidget->clear();//准备SQL语句QString sql = QString("delete from myTable where %1 == numb").arg(numb_ui);//准备语句执行者QSqlQuery querry;if(!querry.exec(sql)){QMessageBox::information(this,"失败","删除失败");return;}else{QMessageBox::information(this,"成功","删除成功");}//准备SQL语句QString sql1 = "select * from myTable";querry.exec(sql1);int i = 0; //记录行号while(querry.next()){for(int j = 0; j<querry.record().count()-1; j++){ui->tableWidget->setItem(i,j,new QTableWidgetItem(querry.record().value(j+1).toString()));}i++; //进行下一行}}else if(!name_ui.isEmpty()){ui->tableWidget->clear();//准备SQL语句QString sql = QString("delete from myTable where '%2' == name").arg(name_ui);//准备语句执行者QSqlQuery querry;if(!querry.exec(sql)){QMessageBox::information(this,"失败","删除失败");return;}else{QMessageBox::information(this,"成功","删除成功");}int i = 0; //记录行号while(querry.next()){for(int j = 0; j<querry.record().count()-1; j++){ui->tableWidget->setItem(i,j,new QTableWidgetItem(querry.record().value(j+1).toString()));}i++; //进行下一行}}else if(score_ui != 0){ui->tableWidget->clear();//准备SQL语句QString sql = QString("delete from myTable where %3 == score").arg(score_ui);//准备语句执行者QSqlQuery querry;if(!querry.exec(sql)){QMessageBox::information(this,"失败","删除失败");return;}else{QMessageBox::information(this,"成功","删除成功");}int i = 0; //记录行号while(querry.next()){for(int j = 0; j<querry.record().count()-1; j++){ui->tableWidget->setItem(i,j,new QTableWidgetItem(querry.record().value(j+1).toString()));}i++; //进行下一行}}else if(!sex_ui.isEmpty()){ui->tableWidget->clear();//准备SQL语句QString sql = QString("delete from myTable where '%4' == sex").arg(sex_ui);//准备语句执行者QSqlQuery querry;if(!querry.exec(sql)){QMessageBox::information(this,"失败","删除失败");return;}else{QMessageBox::information(this,"成功","删除成功");}int i = 0; //记录行号while(querry.next()){for(int j = 0; j<querry.record().count()-1; j++){ui->tableWidget->setItem(i,j,new QTableWidgetItem(querry.record().value(j+1).toString()));}i++; //进行下一行}}}
widget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include<QSqlDatabase> //数据库管理类
#include<QSqlQuery> //执行SQL语句对应的类
#include<QSqlRecord> //记录类
#include<QMessageBox>
#include<QDebug>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private slots:void on_btn1_clicked();void on_btn2_clicked();void on_btn3_clicked();void on_btn4_clicked();private:Ui::Widget *ui;//定义一个数据库对象QSqlDatabase db;
};
#endif // WIDGET_H
tcp服务器
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//给服务器指针实例化对象server = new QTcpServer(this);
}Widget::~Widget()
{delete ui;
}//启动服务器按钮对应的槽函数
void Widget::on_btn_clicked()
{//获取ui界面上的端口号quint16 port = ui->edit->text().toUInt();if(!server->listen(QHostAddress::Any,port)){QMessageBox::critical(this,"失败","服务器启动失败");return;}else{QMessageBox::information(this,"成功","服务器启动成功");}connect(server,&QTcpServer::newConnection,this,&Widget::newConnection_slot);
}//处理的newConnection信号的槽函数的实现
void Widget::newConnection_slot()
{qDebug() << "有新的客户端发来连接请求";QTcpSocket *s = server->nextPendingConnection();//将获取的套接字存放到客户端容器中clientList.push_back(s);connect(s,&QTcpSocket::readyRead,this,&Widget::redyRead_slot);
}//关于readyRead信号的槽函数
void Widget::redyRead_slot()
{//删除客户端链表中的无效客户端套接字for(int i=0;i<clientList.count();i++){if(clientList[i]->state() == 0){clientList.removeAt(i); // 将下标为i的客户端移除掉}}//遍历所有客户端,查看是哪个客户端发来数据for(int i=0;i<clientList.count();i++){if(clientList[i]->bytesAvailable() != 0){QByteArray msg = clientList[i]->readAll();//将数据展示到ui界面上ui->msglist->addItem(QString::fromLocal8Bit(msg));//接接受的消息发送到所有客户端,发送给所有客户端for(int j=0;j<clientList.count();j++){clientList[j]->write(msg);}}}
}
tcp客户端
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//客户端指针实例化空间socket = new QTcpSocket(this);connect(socket,&QTcpSocket::connected,this,&Widget::connected_slot);connect(socket,&QTcpSocket::readyRead,this,&Widget::readyRead_slot);connect(socket,&QTcpSocket::disconnected,this,&Widget::disconnected_slot);
}Widget::~Widget()
{delete ui;
}//连接服务器按钮对应的槽函数
void Widget::on_btn2_clicked()
{//获取ui界面的信息userName = ui->edit2->text(); //获取用户名QString hostName = ui->edit3->text(); //获取主机地址quint16 port= ui->edit4->text().toUInt(); //获取端口号socket->connectToHost(hostName,port);}void Widget::connected_slot()
{QMessageBox::information(this,"成功","连接服务器成功");//顺便向服务器发送一条信息QString msg = userName + ":进入聊天室";socket->write(msg.toLocal8Bit());
}//关于readyRead信号的槽函数
void Widget::readyRead_slot()
{//读取该客户端中的数据QByteArray msg = socket->readAll();//将数据展示在ui界面ui->listWidget->addItem(QString::fromLocal8Bit(msg));
}//发送消息
void Widget::on_btn1_clicked()
{//获取ui界面中编辑文本的内容QString m = ui->edit1->text();//整合要发送的信息QString msg = userName + ":" + m;//将消息发送给服务器socket->write(msg.toLocal8Bit());//将消息编辑框中的内容清空ui->edit1->clear();
}//断开服务器
void Widget::on_btn3_clicked()
{//准备要发送的信息QString msg = userName + ":离开聊天室";socket->write(msg.toLocal8Bit());socket->disconnectFromHost();}void Widget::disconnected_slot()
{QMessageBox::information(this,"退出","退出成功");
}