基于Qt中数据库QSQLITE简单实现员工信息管理
作者:互联网
1、整体界面效果
2、条目UI设计
2.1 自定义条目字段:ID、名字、年龄、地址、薪水
2.2 条目类相关代码
***************************************源文件***********************************************
#include "itemform.h"
#include "ui_itemform.h"
itemForm::itemForm(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::itemForm)
{
ui->setupUi(this);
}
itemForm::~itemForm()
{
delete ui;
}
void itemForm::setStaffInfo(int id, QString name, int age, QString addr, int salary)
{
ui->label_ID->setText(QString::number(id));
ui->label_name->setText(name);
ui->label_age->setText(QString::number(age));
ui->label_address->setText(addr);
ui->label_salary->setText(QString::number(salary));
}
*******************************************头文件*******************************************
#ifndef ITEMFORM_H
#define ITEMFORM_H
#include <QMainWindow>
namespace Ui {
class itemForm;
}
class itemForm : public QMainWindow
{
Q_OBJECT
public:
explicit itemForm(QWidget *parent = nullptr);
~itemForm();
void setStaffInfo(int id,QString name,int age,QString addr,int salary);
private:
Ui::itemForm *ui;
};
#endif // ITEMFORM_H
3、主界面UI设计
3.1 自定义基本的数据操作:添加、更新、删除、查询
3.2 主界面类相关代码
**************************************源文件*********************************************
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//step1:根据数据库的类型,链接数据
db=QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("compary.db");
//step2:打开数据库
if(!db.open()){
qDebug()<<"open error..."<<db.lastError();
}
//step3:根据需求,创建数据库所需要的表
QSqlQuery query;
/**
* @brief sqlCreateTable
* 字段名:id name age address salary
*/
#if 0
QString sqlCreateTable=QString("create table staff(id integer primary key autoincrement,"
"name varchar(20),"
"age int,"
"address varchar(50),"
"salary int)");
if(!query.exec(sqlCreateTable)){
qDebug()<<"create table error"<<db.lastError();
}
#endif
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_addBtn_clicked()
{
//获取UI控件上的内容
QString name=ui->lineEdit_name->text();
QString salary=ui->lineEdit_salary->text();
QString address=ui->lineEdit_address->text();
QString age=ui->lineEdit_age->text();
QSqlQuery query;
//插入数据操作
QString sqlInsert=QString("insert into staff(name,age,address,salary) values('%1',%2,'%3',%4);").arg(name)
.arg(age.toInt())
.arg(address)
.arg(salary.toInt());
if(!query.exec(sqlInsert)){
qDebug()<<"insert data error..."<<db.lastError();
}else{
qDebug()<<"Insert data success..."<<endl;
}
on_selectBtn_clicked();
}
void MainWindow::on_updateBtn_clicked()
{
QSqlQuery query;
QString id=ui->lineEdit_ID->text();
QString address=ui->lineEdit_address->text();
//更新数据操作
QString sqlUpdate=QString("update staff set address = '%1' where id = %2;").arg(address)
.arg(id.toInt());
if(!query.exec(sqlUpdate)){
qDebug()<<"Update data error..."<<db.lastError();
}else{
qDebug()<<"Update data success..."<<endl;
}
on_selectBtn_clicked();
}
void MainWindow::on_deleteBtn_clicked()
{
QSqlQuery query;
QString id=ui->lineEdit_ID->text();
//删除操作
QString sqlDelete=QString("delete from staff where id='%1';").arg(id.toInt());
if(!query.exec(sqlDelete)){
qDebug()<<"Delete data error..."<<db.lastError();
}else{
qDebug()<<"Delete data success...";
}
on_selectBtn_clicked();
}
void MainWindow::on_selectBtn_clicked()
{
//查询数据之前清空列表上的内容
ui->listWidget->clear();
//查询数据操作
QSqlQuery query;
QString sqlSelect=QString("select * from staff;");
if(!query.exec(sqlSelect)){
qDebug()<<"Select data error..."<<db.lastError();
}else{
//遍历数据库记录
while(query.next()){
int id=query.value("id").toInt();
QString name=query.value("name").toString();
int age=query.value("age").toInt();
QString address=query.value("address").toString();
int salary=query.value("salary").toInt();
qDebug()<<"id = "<<id<<", name = "<<name<<", age = "<<age<<", address = "<<address<<", salary = "<<salary;
//每遍历一条记录,就要更新在UI控件上
itemForm* staffitem=new itemForm;
staffitem->setStaffInfo(id,name,age,address,salary);
QListWidgetItem* item=new QListWidgetItem;
item->setSizeHint(QSize(624,55));
ui->listWidget->addItem(item);
ui->listWidget->setItemWidget(item,staffitem);
}
}
}
**************************************头文件*********************************************
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSqlDatabase>
#include <QDebug>
#include <QSqlQuery>
#include <QSqlError>
#include "itemform.h"
#include <QListWidgetItem>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_addBtn_clicked();
void on_updateBtn_clicked();
void on_deleteBtn_clicked();
void on_selectBtn_clicked();
private:
Ui::MainWindow *ui;
QSqlDatabase db;
};
#endif // MAINWINDOW_H
*************************************主函数***********************************************
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
标签:Qt,信息管理,ui,QString,address,query,QSQLITE,include,id 来源: https://blog.csdn.net/Z_AMB_T/article/details/120849639