qt与数据库连接,实现添加、显示、查找、删除功能
weiget.h
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QSqlDatabase> //数据库管理 类
#include <QSqlQuery> //执行sql语句 类
#include <QSqlRecord> //数据库记录 类
#include <QSqlError> //数据库错误 类
#include <QMessageBox>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private slots:void on_addBtn_clicked();void on_showBtn_clicked();void on_selectBtn_clicked();void on_deleteBtn_clicked();private:Ui::Widget *ui;//实例化一个数据库对象QSqlDatabase db;
};
#endif // WIDGET_H
weiget.cpp
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//判断数据库是否存在if(!db.contains()){//说明数据库不存在//创建一个数据库db = QSqlDatabase::addDatabase("QSQLITE"); //表示数据库驱动为sqlite3//给创建的数据库命名db.setDatabaseName("stuInfo.db");QMessageBox::information(this,"","创建数据库成功");}//打开数据库if(!db.open()){QMessageBox::information(this,"","打开数据库失败");return;}//给数据库创建数据库表//实例化一个执行sql语句的类对象QSqlQuery query;//准备sql语句QString sql = "create table if not exists stu_info_table(""id integer primary key autoincrement,""numb integer,""name varchar(20),""sex varchar(4),""score interger)";//执行sql语句if(query.exec(sql)){QMessageBox::information(this,"","创建数据库表成功");}else{QMessageBox::information(this,"","创建数据库表失败");}
}Widget::~Widget()
{delete ui;
}//添加按钮对应的槽函数
void Widget::on_addBtn_clicked()
{//获取ui界面上的信息int numb = ui->numEdit->text().toInt(); //将学号文本转换成整型QString name = ui->nameEdit->text();QString sex = ui->sexEdit->text();int score = ui->scoreEdit->text().toUInt(); //将成绩文本转换成整型//保证用户输入完整信息if(numb == 0 || name.isEmpty() || sex.isEmpty() || score == 0){QMessageBox::information(this,"","请将信息填写完整");return;}//将信息加入到数据库中//需要实例化一个执行sql语句的类对象QSqlQuery query;//准备sql语句QString sql = QString("insert into stu_info_table(numb,name,sex,score)""values(%1,'%2','%3',%4)").arg(numb).arg(name).arg(score);//执行sql语句if(query.exec(sql)){QMessageBox::information(this,"","添加成功");}else{QMessageBox::information(this,"","添加失败");}
}//显示按钮对应的槽函数
void Widget::on_showBtn_clicked()
{//实例化一个执行sql语句的类对象QSqlQuery query;//准备sql语句QString sql = "select * from stu_info_table";//执行sql语句if(query.exec(sql)){//将数据库里的内容放入ui界面上int i = 0; //记录行号while(query.next()) //遍历查询结果{for(int j = 0; j<query.record().count(); j++){ui->tableWidget->setItem(i,j,new QTableWidgetItem(query.value(j+1).toString()));}i++;}}
}//查找按钮对应的槽函数
void Widget::on_selectBtn_clicked()
{//实例化一个执行sql语句的类对象QSqlQuery query;//准备sql语句int numb = ui->numEdit->text().toInt();QString sql = QString("select * from stu_info_table where numb = %1").arg(numb);//执行sql语句if(query.exec(sql)){Widget::on_showBtn_clicked();QMessageBox::information(this,"","查找成功");}else{Widget::on_showBtn_clicked();QMessageBox::information(this,"","查找失败");}
}//删除按钮对应的槽函数
void Widget::on_deleteBtn_clicked()
{//实例化一个执行sql语句的类对象QSqlQuery query;//准备sql语句int numb = ui->numEdit->text().toInt();QString sql = QString("delete from stu_info_table where numb = %1").arg(numb);//执行sql语句if(query.exec(sql)){Widget::on_showBtn_clicked();QMessageBox::information(this,"","删除成功");}else{Widget::on_showBtn_clicked();QMessageBox::information(this,"","删除失败");}
}