基于QT学生管理系统
生活随笔
收集整理的這篇文章主要介紹了
基于QT学生管理系统
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
首先給大家展示成品效果
我把數(shù)據(jù)庫(kù)操作單獨(dú)封裝在一個(gè)文件夾里了如下:
程序源碼://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(); } //在此函數(shù)中,判斷數(shù)據(jù)庫(kù)是否打開,如果打開則關(guān)閉。 void databasetools::closeDatabase() {if(db.isOpen()){db.close();qDebug()<<"close database";} } //調(diào)用getDatabase函數(shù)后,調(diào)用此函數(shù)可以創(chuàng)建一個(gè)表 //表中的字段要求為 學(xué)號(hào)、姓名、年齡、性別、學(xué)院、專業(yè)、地址 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(); }//插入一個(gè)學(xué)生,此函數(shù)的參數(shù)分別為數(shù)據(jù)庫(kù)中的字段值,一一對(duì)應(yīng) 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(); }//根據(jù)學(xué)號(hào)來(lái)刪除一條數(shù)據(jù),參數(shù)是學(xué)號(hào); 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(); }//更新學(xué)號(hào)為no的學(xué)生的信息。(除了學(xué)號(hào)外,其他字段可以更改值) 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(); } //查詢所有學(xué)生,并將所有學(xué)生的數(shù)據(jù)庫(kù)信息組裝為Student對(duì)象后,放入QList來(lái)作為返回值 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; } //使用學(xué)號(hào)no查找固定一個(gè)學(xué)生 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() {}歡迎頁(yè)面:
#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(); }總結(jié)
以上是生活随笔為你收集整理的基于QT学生管理系统的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 服务器打开网页图片显示红x,cacti无
- 下一篇: 数列求和