QT day5

void Widget::on_findbtn_clicked()
{
    int id = ui->idedit->text().toUInt(); //获取ui界面的学号
    QString name = ui->nameedit->text();   //获取ui界面上的姓名
    int score = ui->scoreedit->text().toUInt(); //获取ui界面的分数
    if(id == 0 && name.isEmpty() && score == 0)
    {
        QMessageBox::information(this,"提示","请至少填写一种信息");
        return;
    }
    //准备sql语句
    QString sql1 = QString("select * from myftable where id=%1").arg(id);
    QString sql2 = QString("select * from myftable where name='%1'").arg(name);
    QString sql3 = QString("select * from myftable where score=%1").arg(score);
    //准备语句执行者
    QSqlQuery querry;
    //执行sql语句
    if(querry.exec(sql1))
    {
        int i = 0;
        while(querry.next())
        {
            for(int j=0;jtablewg->setItem(i,j,new QTableWidgetItem(querry.record().value(j+1).toString()));
            }
            i++;
        }
    }
    if(querry.exec(sql2))
    {
        int i = 0;
        while(querry.next())
        {
            for(int j=0;jtablewg->setItem(i,j,new QTableWidgetItem(querry.record().value(j+1).toString()));
            }
            i++;
        }
    }
    if(querry.exec(sql3))
    {
        int i = 0;
        while(querry.next())
        {
            for(int j=0;jtablewg->setItem(i,j,new QTableWidgetItem(querry.record().value(j+1).toString()));
            }
            i++;
        }
    }
    if(querry.exec(sql1) == 0 && querry.exec(sql2) == 0 && querry.exec(sql3) == 0)
    {
        QMessageBox::information(this,"失败","查找失败");
        return;
    }
}

void Widget::on_deletebtn_clicked()
{
    QSqlQuery querry;
    QMessageBox box(QMessageBox::Information,"选项","请选择删除全部或是删除所选",
                    QMessageBox::No|QMessageBox::Yes,this);
    box.setButtonText(QMessageBox::No,"删除所选");
    box.setButtonText(QMessageBox::Yes,"删除全部");
    int ret = box.exec();
    if(ret == QMessageBox::Yes)
    {
        QString sql4 = QString("delete from myftable");
        if(!querry.exec(sql4))
        {
            QMessageBox::information(this,"删除","删除失败");
            return;
        }
        QMessageBox::information(this,"删除","删除成功");
    }
    else
    {
        int id = ui->idedit->text().toUInt(); //获取ui界面的学号
        QString name = ui->nameedit->text();   //获取ui界面上的姓名
        int score = ui->scoreedit->text().toUInt(); //获取ui界面的分数
        if(id == 0 && name.isEmpty() && score == 0)
        {
            QMessageBox::information(this,"提示","请至少填写一种信息");
            return;
        }
        //准备sql语句
        QString sql1 = QString("delete from myftable where id=%1").arg(id);
        QString sql2 = QString("delete from myftable where name='%1'").arg(name);
        QString sql3 = QString("delete from myftable where score=%1").arg(score);
        if(querry.exec(sql1))
        {
            QMessageBox::information(this,"删除","id删除成功");
        }
        if(querry.exec(sql2))
        {
            QMessageBox::information(this,"删除","name删除成功");
        }
        if(querry.exec(sql3))
        {
            QMessageBox::information(this,"删除","score删除成功");
        }
    }
}

你可能感兴趣的:(qt,开发语言)