QT14 how to save data in sqlite database with pushbutton

来源:互联网 发布:央视直播软件下载apk 编辑:程序博客网 时间:2024/06/05 22:09

1. reedit the employinfo window like


2.reedit the employinfo.cpp as


#include "employeeinfo.h"#include "ui_employeeinfo.h"#include "login.h"#include <QMessageBox>EmployeeInfo::EmployeeInfo(QWidget *parent) :    QDialog(parent),    ui(new Ui::EmployeeInfo){    ui->setupUi(this);    if(!login::connOpen())    {        ui->label_sec_status->setText("Failed to open the database") ;    }    else    {        ui->label_sec_status->setText("Connected...") ;    }}EmployeeInfo::~EmployeeInfo(){    delete ui;}void EmployeeInfo::on_pushButton_clicked(){    QString eid, name, surname, age;    eid = ui->txt_eid->text();    name =ui->txt_name->text();    surname = ui->txt_surname->text();    age = ui->txt_age->text();    if(!login::connOpen())    {        qDebug() << "Failed to open the database";        return ;    }    QSqlQuery qry;    QString stmt = "insert into employeeinfo (eid,name,surname,age) values('"+ eid +"','"+ name +"','"+ surname +"','"+ age +"')";    qry.prepare(stmt);    if( qry.exec())    {        QMessageBox::information(this, tr("Save"), tr("Saved"));        login::connClose();    }    else    {        QMessageBox::critical(this, tr("Error"), qry.lastError().text());    }}


0 0
原创粉丝点击