基于QT学生管理系统
生活随笔
收集整理的這篇文章主要介紹了
基于QT学生管理系统
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先給大家展示成品效果
我把數據庫操作單獨封裝在一個文件夾里了如下:
程序源碼://download.csdn.net/download/wanghonghu1/12255206
頭文件:
源文件:
#include "databasetools.h"databasetools::databasetools() {} QSqlDatabase databasetools::db; QList<student> databasetools::list;void databasetools::getDatabase() {db = QSqlDatabase::addDatabase("QSQLITE");db.setDatabaseName("stu_info.db");db.setUserName("123");if(db.isOpen())qDebug()<<"open database";elseqDebug()<<db.lastError(); } //在此函數中,判斷數據庫是否打開,如果打開則關閉。 void databasetools::closeDatabase() {if(db.isOpen()){db.close();qDebug()<<"close database";} } //調用getDatabase函數后,調用此函數可以創建一個表 //表中的字段要求為 學號、姓名、年齡、性別、學院、專業、地址 void databasetools::createTable() {QSqlQuery sql;sql.prepare("CREATE TABLE IF NOT EXISTS student(no INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT NO NULL,age INTEGER,sex TEXT NO NULL,college TEXT NO NULL,major TEXT NO NULL,addr TEXT NO NULL)");if(sql.exec())qDebug()<<"create table sucess";elseqDebug()<<sql.lastError(); }//插入一個學生,此函數的參數分別為數據庫中的字段值,一一對應 void databasetools::insert(QString name, int age, QString sex, QString college, QString major, QString addr) {QSqlQuery sql;sql.prepare("insert into student(name,age,sex,college,major,addr) values(?,?,?,?,?,?)");sql.bindValue(0,name);sql.bindValue(1,age);sql.bindValue(2,sex);sql.bindValue(3,college);sql.bindValue(4,major);sql.bindValue(5,addr);if(sql.exec())qDebug()<<"insert table sucess";elseqDebug()<<sql.lastError(); }//根據學號來刪除一條數據,參數是學號; void databasetools::delete_row(int no) {QSqlQuery sql;sql.prepare("delete from student where no = ?");sql.bindValue(0,no);if(sql.exec())qDebug()<<"delete table sucess";elseqDebug()<<sql.lastError(); }//更新學號為no的學生的信息。(除了學號外,其他字段可以更改值) void databasetools::update(int no, QString name, int age, QString sex, QString college, QString major, QString addr) {QSqlQuery sql;sql.prepare("update student set name = ?,age = ?,sex = ?,college = ?,major = ?,addr = ? where no = ?");sql.bindValue(0,name);sql.bindValue(1,age);sql.bindValue(2,sex);sql.bindValue(3,college);sql.bindValue(4,major);sql.bindValue(5,addr);sql.bindValue(6,no);if(sql.exec())qDebug()<<"update table sucess";elseqDebug()<<sql.lastError(); } //查詢所有學生,并將所有學生的數據庫信息組裝為Student對象后,放入QList來作為返回值 QList<student> databasetools::query() {list.clear();student stu;QSqlQuery sql;sql.prepare("select * from student");if(sql.exec()){while(sql.next()){stu.no= sql.value(0).toInt();stu.name= sql.value(1).toString();stu.age= sql.value(2).toInt();stu.sex= sql.value(3).toString();stu.college= sql.value(4).toString();stu.major= sql.value(5).toString();stu.addr= sql.value(6).toString();list.append(stu);}}return list; } //使用學號no查找固定一個學生 student databasetools::query(int no) {student stu;QSqlQuery sql;sql.prepare("select * from student where no = ?");sql.bindValue(0,no);sql.exec();qDebug()<<no;if(sql.first()){stu.no<<sql.value(0).toInt();stu.name= sql.value(1).toString();stu.age= sql.value(2).toInt();stu.sex= sql.value(3).toString();stu.college= sql.value(4).toString();stu.major= sql.value(5).toString();stu.addr= sql.value(6).toString(); // int no = sql.value(0).toInt();}elseqDebug()<<sql.lastError();return stu; } databasetools::~databasetools() {}歡迎頁面:
#ifndef WELCOME_H #define WELCOME_H#include <QDialog> #include"dialog.h" #include<QTimer> namespace Ui { class welcome; }class welcome : public QDialog {Q_OBJECTpublic:explicit welcome(QWidget *parent = 0);~welcome();private slots:void on_pushButton_clicked();private:Ui::welcome *ui;Dialog *s;};#endif // WELCOME_H#include "welcome.h" #include "ui_welcome.h"welcome::welcome(QWidget *parent) :QDialog(parent),ui(new Ui::welcome) {ui->setupUi(this);s = new Dialog(this);setWindowTitle("王洪虎"); }welcome::~welcome() {delete ui; }void welcome::on_pushButton_clicked() {s->show();this->hide(); }總結
以上是生活随笔為你收集整理的基于QT学生管理系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 服务器打开网页图片显示红x,cacti无
- 下一篇: 在移动硬盘(U盘)上安装最新版Windo