Qt15 Update a data from Sqlite database with pushbutton
2016-03-31 01:19
477 查看
1. reedit the employeeinfo window as
2. modify the employeeinfo.cpp
#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());
}
}
void EmployeeInfo::on_pushButton_edit_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 = "update employeeinfo set eid='"+ eid+"',name='"+ name+ "',surname='"+ surname+"',age='"+ age +"'where eid='" + eid + "'";
qry.prepare(stmt);
if( qry.exec())
{
QMessageBox::information(this, tr("Edit"), tr("Updated"));
login::connClose();
}
else
{
QMessageBox::critical(this, tr("Error"), qry.lastError().text());
}
}
2. modify the employeeinfo.cpp
#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());
}
}
void EmployeeInfo::on_pushButton_edit_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 = "update employeeinfo set eid='"+ eid+"',name='"+ name+ "',surname='"+ surname+"',age='"+ age +"'where eid='" + eid + "'";
qry.prepare(stmt);
if( qry.exec())
{
QMessageBox::information(this, tr("Edit"), tr("Updated"));
login::connClose();
}
else
{
QMessageBox::critical(this, tr("Error"), qry.lastError().text());
}
}
相关文章推荐
- QT14 how to save data in sqlite database with pushbutton
- Qt浅谈之四十七下拉列表菜单
- QT5: QDialog.
- VS2013+Qt5.6.0+opencv3.0环境配置
- QT5.6 编译SQLServer驱动
- qt4与qt5不兼容问题
- 一个电网QT项目的开篇
- QTP 导入 XML作为环境变量
- QT版DomainName软件导出BUG
- 关于QT窗口
- Qt5.6 windows下vs2015编译QODBC
- QT:Qt4添加QSerialPort模块
- Qt判断进程是否运行
- QT:QT编译时 qmake错误
- QT:配置qtcreator交叉环境
- Qt Creator 窗体控件自适应窗口大小布局
- Qt之QTableView
- Qt之线程(QThread)
- Qt中正确的设置窗体的背景图片的几种方式
- Qt下载地址