1、首先在.pro文件中加入 QT += sql widgets
2、创建连接类connection.h
static bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("my.db");
if(!db.open())
{
QMessageBox::critical(0,"Cannot open database1","Unable to establish a database",QMessageBox::Cancel);
return false;
}
QSqlQuery query;
//创建student表
query.exec("create table student(id int primary key,name varchar,course int");
query.exec("insert into student values(,'李强',11)");
query.exec("insert into student values(2,'马良',11)");
query.exec("insert into student values(3,'孙红',12)");
//创建course表
query.exec("create table course (id int primary key,name varchar,teacher varchar)");
query.exec("insert into course values(10,'数学','王老师')");
query.exec("create into course values (11,'英语','张老师')");
query.exec("insert into course values(12,'计算机','白老师')");
return true;
}
3、mainwindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();//修改提交
void on_pushButton2_clicked();//撤销修改
void on_pushButton3_clicked();//添加记录
void on_pushButton4_clicked();//删除选中行
void on_pushButton5_clicked();//按id升序排列
void on_pushButton6_clicked();//按id 降序排列
void on_pushButton7_clicked();//查询
void on_pushButton8_clicked();//显示列表
private:
Ui::MainWindow *ui;
QSqlTableModel * model;
};
4、mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QSqlQueryModel>
#include<QSqlTableModel>
#include<QSqlRelationalTableModel>
#include<QTableView>
#include<QMessageBox>
#include<QSqlError>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
model = new QSqlTableModel(this);
model ->setTable("student");
model->select();
//设置编辑策略
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
ui->tableView->setModel(model);
}
MainWindow::~MainWindow()
{
delete ui;
}
//修改提交
void MainWindow::on_pushButton_clicked()
{
//开始事务操作
model->database().transaction();
if(model->submitAll())
{
QMessageBox::information(this,tr("tableModel"),tr("数据修改成功"));
}
else
{
model->database().rollback(); //回滚
QMessageBox::warning(this,tr("tableModel"),tr("数据库错误:%1").arg(model->lastError().text()),
QMessageBox::Ok);
}
}
//撤销修改
void MainWindow::on_pushButton2_clicked()
{
model->revertAll();//事件回滚
}
//添加记录
void MainWindow::on_pushButton3_clicked()
{
//获取表的行数
int rowNum = model->rowCount();
int id = 10;
//添加一行
model->insertRow(rowNum);
model->setData(model->index(rowNum,0),id);
//可以直接提交
model->submitAll();
}
//删除选中行
void MainWindow::on_pushButton4_clicked()
{
//获取选中的行
int curRow = ui->tableView->currentIndex().row();
//删除该行
model->removeRow(curRow);
int ok = QMessageBox::warning(this,tr("删除当前行!"),
tr("你确定删除当前行吗?"),QMessageBox::Yes,QMessageBox::No);
if(ok == QMessageBox::No)
{
//如果不删除则撤销
model->revertAll();
}
else //否则,在数据库中删除该行
{
model->submitAll();
}
}
//按id升序排列
void MainWindow::on_pushButton5_clicked()
{
//id字段 即第0列,升序排列
model->setSort(0,Qt::AscendingOrder);
model->select();
}
//按id 降序排列
void MainWindow::on_pushButton6_clicked()
{
model->setSort(0,Qt::DescendingOrder);
model->select();
}
//查询
void MainWindow::on_pushButton7_clicked()
{
QString name = ui->lineEdit->text();
//根据姓名进行筛选,一定要使用单引号
model->setFilter(QString("name = '%1").arg(name));
model->select();
}
//显示列表
void MainWindow::on_pushButton8_clicked()
{
model->setTable("student");
model->select();
}
5、在main.cpp中添加 if(!createConnection()) return 1;
6、mainwindow.ui
原文作者:临渊慎行
原文地址:https://blog.csdn.net/zq9955/article/details/115052127