QSqlTableModel
The QSqlTableModel class provides an editable data model for a single database table.
QSqlTableModel is a high-level interface for reading and writing database records from a single table. It is built on top of the lower-level QSqlQuery and can be used to provide data to view classes such as QTableView.
Example:
QSqlTableModel *model = new QSqlTableModel(parentObject, database);
model->setTable("employee");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
model->setHeaderData(0, Qt::Horizontal, tr("Name"));
model->setHeaderData(1, Qt::Horizontal, tr("Salary"));
QTableView *view = new QTableView;
view->setModel(model);
view->hideColumn(0); // don't show the ID
view->show();
From the last blog:
http://blog.csdn.net/u013402772/article/details/55212464
It can be seen that although QSqlQuery Model can be used to implement editable functions, it is more difficult to implement. QSqlTable Model provides a read-write model that can only operate on a single SQL table at a time. It is a higher-level alternative to QSqlQuery. It can browse and modify independent SQL tables, and it only needs to write very little code, and does not need to understand the SQL syntax.
Write a Demo that contains some common operations:
The interface is as follows:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QSqlError>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
model = new QSqlTableModel(this);
model->setTable("student");
//Setting up a save policy
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select(); //Query the whole table
ui->tableView->setModel(model);
//ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
}
MainWindow::~MainWindow()
{
delete ui;
}
After creating a QSqlTableModel, you simply use setTable() to specify the database table for it, and then use the select() function to query. Calling these two functions is equivalent to executing the select * from student SQL statement. You can also use setFilter() here to specify the conditions for queries, and you'll see the use of this function later.
Before using this model, we usually set up its editing strategy, which is defined by QSqlTableModel::EditStrategy enumeration variable, and has three values, as shown in the following figure. Used to indicate when changes are submitted when the values in the database are edited.
//Submit revision
void MainWindow::on_pushButton_clicked(bool checked)
{
model->database().transaction();//Start things up
if(model->submitAll())
model->database().commit();
else{
model->database().rollback(); //RollBACK
QMessageBox::warning(this,tr("tableModel"),
QStringLiteral("Database error:")+tr("%1").arg(model->lastError().text()));
}
}
//Revocation
void MainWindow::on_pushButton_2_clicked(bool checked)
{
model->revertAll();
//If the database is submitted, it will not be modified.
}
//query
void MainWindow::on_pushButton_7_clicked()
{
QString name = ui->lineEdit->text();
//screen
model->setFilter(QString("name = '%1'").arg(name));
//query
model->select();
}
//Display full table
void MainWindow::on_pushButton_8_clicked()
{
//Re Association
model->setTable("student");
//query
model->select();
}
//According to id ascending order
void MainWindow::on_pushButton_5_clicked()
{
model->setSort(0,Qt::AscendingOrder); //Column 0 ascending order
model->select();
}
//Descending by id
void MainWindow::on_pushButton_6_clicked()
{
model->setSort(0,Qt::DescendingOrder);//Column 0 descending
model->select();
}
//Delete selected rows
void MainWindow::on_pushButton_4_clicked()
{
//Get the selected row
int curRow = ui->tableView->currentIndex().row();
//Delete the row
model->removeRow(curRow); //It's just deleted from the model and not submitted to the database yet.
int ok = QMessageBox::warning(this,QStringLiteral("Delete the current line"),
QStringLiteral("Are you sure?"),
QMessageBox::Yes,QMessageBox::No);
if(ok == QMessageBox::No)
model->revertAll();
else
model->submitAll();
}
//Add record
void MainWindow::on_pushButton_3_clicked()
{
int rowCount = model->rowCount();
int id = 10;
model->insertRow(rowCount);
model->setData(model->index(rowCount,0),id);
//model->submit();
}
Add a row at the end of the table, because we set the id number as the primary key in the student table, so we must use the setData() function to add the value of the id attribute to the newly added row, otherwise adding rows will not succeed. The submitAll() function can be invoked directly for submission, or the submit modification button can be used for submission.
Conclusion:
You can see that this model is very powerful, and completely separated from the SQL statement, even if you don't know much about the database knowledge, you can use it to do most of the commonly used operations. We also see that this model provides a buffer for saving the changes first, and then we can really modify the database when we execute the submission function. Of course, this model is more advanced than the previous model, and all the operations mentioned above can be performed here.