0115qt聊天室客户端+数据库

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include //数据库管理类
#include //执行sql语句类
#include //数据库记录类
#include //数据库错误类
#include //消息对话框类

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_addBtn_clicked();

    void on_showBtn_clicked();

    void on_delBtn_clicked();

    void on_updateBtn_clicked();

private:
    Ui::Widget *ui;
    //实例化一个数据库对象
    QSqlDatabase db;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //判断是否有该数据库
    if(!db.contains("stuInfo.db"))
    {
        //说明不存在数据库,则创建
        db=QSqlDatabase::addDatabase("QSQLITE");//增加一个数据库,驱动为sqlite3
        //给创建的数据库命名
        db.setDatabaseName("stdInfo");
    }
    //打开数据库
    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 integer)";
    //执行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->numberEdit->text().toUInt();
    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(sex).arg(score);//arg()的返回值是自身的引用,所以可以接着用
    //执行sql语句
    if(query.exec(sql))
    {
        QMessageBox::information(this,"","添加成功!");
    }
    else
    {
        QMessageBox::information(this,"","添加失败!");
    }
}
//显示按钮对应的槽函数处理
void Widget::on_showBtn_clicked()
{
    ui->tableWidget->clear();
    //准备执行sql的对象
    QSqlQuery query;
    QString sql="select *from stu_info_table";
    //如果查询失败
    if(!query.exec(sql))
    {
         QMessageBox::information(this,"","查询失败!");
         return;
    }
    //查询成功,所查询的信息就已经存放到query中了
    //遍历
    int i=0;//记录行
    int j=0;//记录列
    while(query.next())
    {
        for(j=0;jtableWidget->setItem(i,j,new QTableWidgetItem(query.value(j+1).toString()));

        }
        i++;//行数递增
    }
}
//删除按钮对应的槽函数处理
void Widget::on_delBtn_clicked()
{
    //准备执行sql语句的对象
    QSqlQuery query;

    int numb=ui->numberEdit->text().toUInt();
    QString sql=QString("delete from stu_info_table where  numb=%1 ").arg(numb);

    //判断删除成功失败
    if(!query.exec(sql))
    {
        QMessageBox::information(this,"","删除失败!");
        return;
    }

}
//修改按钮对应的槽函数处理
void Widget::on_updateBtn_clicked()
{
    //准备执行sql语句的对象
    QSqlQuery query;
    int numb=ui->numberEdit->text().toUInt();
    QString name=ui->nameEdit->text();
    QString sex=ui->sexEdit->text();
    int score=ui->scoreEdit->text().toUInt();
    //判断用户是否信息是否填写完整
    if(numb==0||name.isEmpty())
    {
        //如果信息不完整,提示,并退出
        QMessageBox::information(this,"","请填入修改的信息");
        return;

    }
    QString sql=QString("update stu_info_table set name='%1' where numb=%2").arg(name).arg(numb);
    if(!query.exec(sql))
    {
        QMessageBox::information(this,"","修改失败!");
        return;
    }

}

0115qt - 幕布

你可能感兴趣的:(数据库)