1.首先创建工程文件,在创建过程中添加主窗口类以及对应的ui文件。
下面分别实现上图中4个按钮的功能。我们用MYSQL来对学生的注册信息进行保存。
2.编写与MYSQL相关程序
创建类过程如下:
#ifndef MYSQL_H
#define MYSQL_H
#include
#include
#include
#include
#include "mainwindow.h"
class MySql
{
public:
MySql();
void initsql(); //初始化连接数据库
void createtable(); //建表 索引 触发器
private:
QSqlQuery *query;
};
#endif // MYSQL_H
#include "mysql.h"
MySql::MySql()
{
}
void MySql::initsql()
{
QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("127.0.0.1");
db.setUserName("root");
db.setPassword("password");
db.setDatabaseName("Stu");
if(db.open())
{
qDebug()<<"Database connected successfully!";
createtable();
return;
}
else
{
qDebug()<<"Database connected failed!";
return;
}
}
void MySql::createtable()
{
query=new QSqlQuery;
/*用户表*/
query->exec("create table user(name VARCHAR(30) UNIQUE NOT NULL, passward VARCHAR(30), identify VARCHAR(30) PRIMARY KEY)");
/*学生信息表*/
query->exec("create table studentInfo(stuaccount VARCHAR(30) PRIMARY KEY UNIQUE NOT NULL, stuname VARCHAR(30), stusex ENUM('男', '女') NOT NULL, stuage INT, identify VARCHAR(30) UNIQUE NOT NULL, tel VARCHAR(20), enroll_time DATE, leave_time DATE, scondition enum('学习', '结业', '退学'), stu_text TEXT)");
/*体检信息表*/
query->exec("create table healthInfo(stuaccount VARCHAR(30) PRIMARY KEY UNIQUE NOT NULL, stuname VARCHAR(30) NOT NULL, height FLOAT, weight FLOAT, differentiate ENUM('正常', '色弱', '色盲'), left_sight FLOAT, right_sight FLOAT, left_ear ENUM('正常','偏弱'), right_ear ENUM('正常','偏弱'), legs ENUM('正常','不相等'), pressure ENUM('正常','偏高','偏低'), history VARCHAR(50), h_text TEXT)");
/*考试课程表*/
query->exec("create table courseInfo(classid INT PRIMARY KEY UNIQUE NOT NULL AUTO_INCREMENT, classname VARCHAR(30), beforeclassid INT)");
/*考试成绩表*/
query->exec("create table gradeInfo(stuaccount VARCHAR(30) PRIMARY KEY UNIQUE NOT NULL, classid INT NOT NULL, lasttime date, times INT default 1, grade float default 0, constraint grade_acc_fk FOREIGN KEY(stuaccount) REFERENCES studentInfo(stuaccount), constraint grade_cid_fk FOREIGN KEY(classid) REFERENCES courseInfo(classid))");
/*领证时间表*/
query->exec("create table licenseInfo(stuaccount VARCHAR(30) PRIMARY KEY UNIQUE NOT NULL, stuname VARCHAR(30), recv_time date, recv_name VARCHAR(30), l_text TEXT);");
/*创建root用户*/
query->exec("insert into user value('root', 'root', 'root')");
/*学生表姓名索引*/
query->exec("create index int_name ON studentInfo(stuname)");
/*学生表学习状态索引*/
query->exec("alter table studentInfo ADD INDEX ind_con(scondition)");
/*体检表姓名索引*/
query->exec("create index ind_hname ON healthInfo(stuname)");
/*领证表姓名索引*/
query->exec("alter table licenseInfo ADD INDEX ind_lname(stuname)");
/*领证表领证人索引*/
query->exec("alter table licenseInfo ADD INDEX ind_recvname(recv_name)");
/*设计视图*/
query->exec("create VIEW grade_view AS SELECT g.stuaccount, s.stuname, c.classname, g.lasttime, g.times, g.grade FROM studentinfo s, courseinfo c, gradeinfo g WHERE g.stuaccount = s.stuaccount AND g.classid = c.classid");
/*设计insert触发器*/
query->exec("CREATE TRIGGER license_stu AFTER INSERT ON licenseInfo FOR EACH ROW BEGIN UPDATE studentInfo SET leave_time=NEW.recv_time, scondition = '结业' WHERE stuaccount = NEW.stuaccount; END");
/*设计update触发器*/
query->exec("CREATE TRIGGER update_name AFTER UPDATE ON studentinfo FOR EACH ROW BEGIN UPDATE healthinfo SET stuname = NEW.stuname WHERE stuaccount = NEW.stuaccount;END");
/*设计delete触发器*/
query->exec("CREATE TRIGGER delete_stu AFTER DELETE ON studentinfo FOR EACH ROW BEGIN DELETE FROM gradeinfo WHERE stuaccount = OLD.stuaccount; DELETE FROM healthinfo WHERE stuaccount = OLD.stuaccount; DELETE FROM licenseinfo WHERE stuaccount = OLD.stuaccount;END");
}
同时在main.cpp中加入:
MySql MS;
MS.initsql();
下一步,实现注册功能,便捷注册界面。
3.编辑注册界面
register.ui界面如下:
注册界面做好后,下面我们编写将注册信息添加到数据库相关程序。
4.修改mysql.h和mysql.cpp,添加注册信息到数据库
将注册信息添加到数据库代码如下:
bool MySql::inituser(QString name,QString identify,QString passward)
{
query=new QSqlQuery;
QString str=QString("insert into user value('%1','%2','%3')").arg(name).arg(identify).arg(passward);
bool ret=query->exec(str);
return ret;
}
MySql* mysql = new MySql;
bool ret = mysql->inituser(name, identify, passward);
注册后的效果图如下:
我们可以在数据库中查到新添加的信息:
下一步,实现登录功能。
5.编辑登录界面
登录界面如下:
为了实现登录功能,我们先编写验证用户名和密码与数据库是否匹配函数。
bool MySql::loguser(QString name,QString passward)
{
query=new QSqlQuery;
QString str=QString("select * from user where name= '%1' and passward = '%2' ").arg(name).arg(passward);
query->exec(str);
query->last();
int record=query->at()+1;
if(record==0)
return false;
return true;
}
登录成功界面如下:
6.实现找回密码功能
void MainWindow::on_forgetPasswdBtn_clicked()
{
bool ok;
QInputDialog ecf;
name = ecf.getText(this,tr("忘记密码"),tr("请输入用户名:"),QLineEdit::Normal,NULL,&ok);
identify=ecf.getText(this,tr("忘记密码"),tr("请输入证件号:"),QLineEdit::Normal,NULL,&ok);
MySql mysql;
bool ret=mysql.forgetpassward(name,identify);
if(!ret)
{
QMessageBox::information(this,tr("消息"),tr("消息不匹配!"),QMessageBox::Ok);
return;
}
QMessageBox::information(this,tr("消息"),tr("请妥善保管您的密码:%1").arg(passward),QMessageBox::Ok);
return;
}
void MainWindow::on_exitBtn_clicked()
{
this->close();
}
8.下面实现登录成功后学生信息管理功能界面
登录成功后出现的界面如下:
下面分别实现各个管理模块即可。
9.实现学籍管理模块
学籍管理模块界面如下:
10.实现添加学生信息功能
由于篇幅,后面内容不再赘述,完整项目见
https://github.com/onlyou2030/Student-Information-Manage-System