- 添加数据
前面说到了如何连接数据库,并查询指定内容。为了能实现数据输入,本文直接新建一个对话框 来进行数据输入,通过SQL语句将数据写进表中。这里新添加了一个界面类addDialog,如下: - 实现
**adddialog.h文件**
#ifndef ADDDIALOG_H
#define ADDDIALOG_H
#include <QDialog>
#include <QLineEdit>
#include <QLabel>
namespace Ui {
class addDialog;
}
class addDialog : public QDialog
{
Q_OBJECT
public:
explicit addDialog(QWidget *parent = 0);
~addDialog();
private slots:
void addData();
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
Ui::addDialog *ui;
QLineEdit *lineEdit;
QLineEdit *lineEdit_2;
QLineEdit *lineEdit_3;
QLineEdit *lineEdit_4;
QLabel *label_6;
};
#endif // ADDDIALOG_H
接着,cpp实现文件:
// adddialog.cpp 文件
#include "adddialog.h"
#include "ui_adddialog.h"
#include <QSqlQuery>
#include <QDebug>
#include <QMessageBox>
addDialog::addDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::addDialog)
{
ui->setupUi(this);
}
addDialog::~addDialog()
{
delete ui;
}
void addDialog::addData(){
this->show();
QSqlQuery testQuery;
if