本文记录使用QSqlQuery实现增删改查的过程。
目录
通过dialog更新返回的数据,使用update语句进行更新
1. 构建表格数据
声明变量
QSqlQueryModel *qryModel;//数据查询模型
QItemSelectionModel *selModel;//选择模型
SQLiteHelper dbHelper;//自定义数据库帮助类
表格、数据模型、选择模型三板斧设置
qryModel = new QSqlQueryModel(this);
selModel = new QItemSelectionModel(qryModel,this);
ui->tableView->setModel(qryModel);
ui->tableView->setSelectionModel(selModel);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
列表执行查询
qryModel->setQuery("SELECT empNo,Name,Gender,Birthday,Province, Department,"
" Salary FROM employee order by empNo");
if(qryModel->lastError().isValid())
{
QMessageBox::information(this,"错误","数据表查询失败:\n"+qryModel->lastError().text());
return;
}
列表的水平表头设置
//设置表头
QSqlRecord rec = qryModel->record();//获取空记录
qryModel->setHeaderData(rec.indexOf("empNo"),Qt::Horizontal,"工号");
qryModel->setHeaderData(rec.indexOf("Name"), Qt::Horizontal, "姓名");
qryModel->setHeaderData(rec.indexOf("Gender"), Qt::Horizontal, "性别");
qryModel->setHeaderData(rec.indexOf("Birthday"), Qt::Horizontal, "出生日期");
qryModel->setHeaderData(rec.indexOf("Province"), Qt::Horizontal, "省份");
qryModel->setHeaderData(rec.indexOf("Department"), Qt::Horizontal, "部门");
qryModel->setHeaderData(rec.indexOf("Salary"), Qt::Horizontal, "工资");
2. 新增一行
构建一个空行
QSqlQuery query;
query.exec("select * from employee where empNo=-1");
QSqlRecord curRec = query.record();
curRec.setValue("empNo",qryModel->rowCount()+3000);
通过dialog返回的修改行数据,update更新
TDialogData *dlg = new TDialogData(this);
Qt::WindowFlags flags = dlg->windowFlags();
dlg->setWindowFlags(flags|Qt::MSWindowsFixedSizeDialogHint);//对话框固定大小
dlg->setInsertRecord(curRec); //打开窗口,传递插入记录 模式
if(dlg->exec()!=QDialog::Rejected)
{
QSqlRecord recData= dlg->getRecordData();//回传回来 记录值
query.prepare("INSERT INTO employee(empNo,Name,Gender,Birthday,Province,Department,Salary,Memo,Photo)"
" Values(:empNo,:Name,:Gender,:Birthday,:Province,:Department,:Salary,:Memo,:Photo)");
query.bindValue(":empNo",recData.value("empNo"));
query.bindValue(":Name",recData.value("Name"));
query.bindValue(":Gender",recData.value("Gender"));
query.bindValue(":Birthday",recData.value("Birthday"));
query.bindValue(":Province",recData.value("Province"));
query.bindValue(":Department",recData.value("Department"));
query.bindValue(":Salary",recData.value("Salary"));
query.bindValue(":Memo",recData.value("Memo"));
query.bindValue(":Photo",recData.value("Photo"));
if(!query.exec())
QMessageBox::critical(this,"错误","插入记录失败:\n"+query.lastError().text());
else
{
QMessageBox::information(this,"提示","插入完成");
QString sqlStr = qryModel->query().executedQuery();//执行过的查询
qryModel->setQuery(sqlStr);
}
}
delete dlg;
3. 更新一行
获取到需要更新的行
QSqlRecord curRec = qryModel->record(recNo);
int empNo = curRec.value("empNo").toInt();
QSqlQuery query;
query.prepare("select * from employee where empNo=:ID");
query.bindValue(":ID",empNo);
query.exec();
query.first();
if(!query.isValid())//无有效记录
return;
curRec =query.record();//获取当前的记录
通过dialog更新返回的数据,使用update语句进行更新
TDialogData *dlg = new TDialogData(this);
Qt::WindowFlags flags = dlg->windowFlags();
dlg->setWindowFlags(flags |Qt::MSWindowsFixedSizeDialogHint);//对话框固定大小
dlg->setUpdateRecord(curRec);
int ret = dlg->exec();
if(ret == QDialog::Accepted)
{
QSqlRecord recData = dlg->getRecordData();
query.prepare("update employee set Name =:Name,Gender =:Gender,"
" Birthday=:Birthday,Province =:Province,"
" Department=:Department,Salary=:Salary,"
" Memo=:Memo,Photo=:Photo"
" where empNo=:ID");
query.bindValue(":ID",empNo);
query.bindValue(":Name",recData.value("Name"));
query.bindValue(":Gender",recData.value("Gender"));
query.bindValue(":Birthday",recData.value("Birthday"));
query.bindValue(":Province",recData.value("Province"));
query.bindValue(":Department",recData.value("Department"));
query.bindValue(":Salary",recData.value("Salary"));
query.bindValue(":Memo",recData.value("Memo"));
query.bindValue(":Photo",recData.value("Photo"));
if(!query.exec())
QMessageBox::critical(this,"错误","更新错误:\n"+ query.lastError().text());
else
{
QString strSql = qryModel->query().executedQuery();//数据模型刷新数据
qryModel->setQuery(strSql);
}
}
delete dlg;
4.删除一行
获取该行,及主键
int curRecNo = selModel->currentIndex().row();
QSqlRecord recData= qryModel->record(curRecNo);
int empNo = recData.value("empNo").toInt();
以主键为参数,删除行
QSqlQuery query;
query.prepare("delete from employee where empNo=:empNo");
query.bindValue(":empNo",empNo);
if(!query.exec())
QMessageBox::critical(this,"错误提示","删除失败:\n"+query.lastError().text());
else
{
QString strSql = qryModel->query().executedQuery();
qryModel->setQuery(strSql);//刷新上一次查询的查询结果
}
5. 批量更改
通过执行批量更改的sql语句达到批改的目的
更新操作受影响行数
QSqlQuery query;
query.prepare("update employee set Salary=Salary*1.1");
if(!query.exec())
QMessageBox::critical(this,"错误提示","更新失败:\n"+query.lastError().text());
else
{
qint64 affectedRows = query.numRowsAffected();
qDebug() << "更新操作影响的行数:" << affectedRows;
QString strSql = qryModel->query().executedQuery();
qryModel->setQuery(strSql);//刷新上一次查询的查询结果
}
总结
1. QSqlQuery是能执行任何SQL语句的类,如SELECT、INSERT、UPDATE、DELETE等SQL语句。
2. 列表查询展示使用QSqlQueryModel 类,使用QSqlQuery来执行sql语句。
tips:
执行上一次的查询
QString strSql = qryModel->query().executedQuery();
qryModel->setQuery(strSql);//刷新上一次查询的查询结果