当前位置: 首页 > news >正文

QT--SQLite

配置类相关的表,所以我使用sqlite,且QT自带该组件;

1.安装 sqlite-tools-win-x64-3460000、SQLiteExpert5.4.31.575
使用SQLiteExpert建好数据库.db文件,和对应的表后把db文件放在指定目录 ./db/program.db;

2.选择sql组件

在这里插入图片描述
3.新增数据库处理类,在使用数据库的地方调用类成员函数即可

DataModel::DataModel()
{db = QSqlDatabase::addDatabase("QSQLITE", "");db.setDatabaseName("./db/program.db");connectDataBase();
}DataModel::~DataModel()
{disconnectDataBase();
}// 打开数据库文件
bool DataModel::connectDataBase() {bool ret = db.open();if (!ret) {outPutMsg(QtDebugMsg, "DataModel::connectDataBase error = " + db.lastError().text());} return ret;
}
// 关闭数据库文件
void DataModel::disconnectDataBase() {db.close();
}
QStringList DataModel::queryProgramList() {QStringList programList;// 根据名字查询QString sql = QString("select sectionBarName,programNo from program where 1=1;");outPutMsg(QtDebugMsg, "DataModel::queryProgramList sql = " + sql);// 创建一个可以对db执行语句的对象QSqlQuery query(db);// 执行sql语句bool ret = query.exec(sql);if (!ret){outPutMsg(QtDebugMsg, "DataModel::queryProgramNo error = " + query.lastError().text());return programList;}// 行坐标向下移while (query.next()){//获取数据库query所指的那行的数据QString program;program += (query.value(0).toString() + ";"); // sectionBarNameprogram += QString::number(query.value(1).toInt()); // programNo// outPutMsg(QtDebugMsg, "DataModel::queryProgramInfoBySectionCode programInfo = " + program);programList.append(program);}return programList;
}
// 根据型材代号查询程序号码
QString DataModel::queryProgramNo(QString sectionBarNo) {// 根据名字查询QString sql = QString("select programNo from program where sectionBarName=\'%1\';").arg(sectionBarNo);//QString sql = QString("select programNo from program where 1=1");outPutMsg(QtDebugMsg, "DataModel::queryProgramNo sql = " + sql);// 创建一个可以对db执行语句的对象QSqlQuery query(db);// 执行sql语句bool ret = query.exec(sql);if (!ret){outPutMsg(QtDebugMsg, "DataModel::queryProgramNo error = " + query.lastError().text());return "";}QString programNo = "";// 行坐标向下移while (query.next()){//获取数据库query所指的那行的数据programNo = QString::number(query.value(0).toInt());outPutMsg(QtDebugMsg, "DataModel::queryProgramNo programNo = " + programNo);}return programNo;
}// 根据程序号码查询
QString DataModel::queryProgramByNo(QString programNo) {// 根据名字查询QString sql = QString("select sectionBarName from program where programNo=\'%1\';").arg(programNo);//QString sql = QString("select programNo from program where 1=1");outPutMsg(QtDebugMsg, "DataModel::queryProgramByNo sql = " + sql);// 创建一个可以对db执行语句的对象QSqlQuery query(db);// 执行sql语句bool ret = query.exec(sql);if (!ret){outPutMsg(QtDebugMsg, "DataModel::queryProgramByNo error = " + query.lastError().text());return "";}QString sectionBarNo = "";// 行坐标向下移while (query.next()){//获取数据库query所指的那行的数据sectionBarNo = QString::number(query.value(0).toInt());outPutMsg(QtDebugMsg, "DataModel::queryProgramByNo sectionBarNo = " + sectionBarNo);}return sectionBarNo;
}// 根据型材代号查询程序号码
QStringList DataModel::queryProgramInfoBySectionCode(QString sectionBarNo) {QStringList programInfoList;// 根据名字查询// 使用索引sectionBarNameQString sql = QString("select stepNo, cutNo,x, y, f, Rx, Ry from programInfo where programNo = (select programNo from program where sectionBarName=\'%1\');").arg(sectionBarNo);//QString sql = QString("select programNo from program where 1=1");outPutMsg(QtDebugMsg, "DataModel::queryProgramInfoBySectionCode sql = " + sql);// 创建一个可以对db执行语句的对象QSqlQuery query(db);// 执行sql语句bool ret = query.exec(sql);if (!ret){outPutMsg(QtDebugMsg, "DataModel::queryProgramInfoBySectionCode error = " + query.lastError().text());return programInfoList;}// 行坐标向下移while (query.next()){//获取数据库query所指的那行的数据QString programInfo;programInfo += (QString::number(query.value(0).toInt())+";"); // stepNoprogramInfo += (QString::number(query.value(1).toInt())+";"); // cutNoprogramInfo += (QString::number(query.value(2).toDouble(), 'f', 2) + ";"); // xprogramInfo += (QString::number(query.value(3).toDouble(), 'f', 2) + ";"); // yprogramInfo += (QString::number(query.value(4).toDouble(), 'f', 2) + ";"); // f 速度programInfo += (QString::number(query.value(5).toDouble(), 'f', 2) + ";"); // RxprogramInfo += (QString::number(query.value(6).toDouble(), 'f', 2)); // RyoutPutMsg(QtDebugMsg, "DataModel::queryProgramInfoBySectionCode programInfo = " + programInfo);programInfoList.append(programInfo);}return programInfoList;
}// 更新程序信息
bool DataModel::updateProInfosByProNo(QString proNo, QStringList proInfoList) {// 开启事务if (!db.transaction()) {outPutMsg(QtDebugMsg, "DataModel::updateProInfosByProNo db.error = " + db.lastError().text());return false;}QStringList programInfoList;// 根据名字查询// 使用索引sectionBarNameQString sqlDel = QString("delete from programInfo where programNo=\'%1\';").arg(proNo);outPutMsg(QtDebugMsg, "DataModel::updateProInfosByProNo sqlDel = " + sqlDel);// 创建一个可以对db执行语句的对象QSqlQuery query(db);// 执行sql语句bool ret = query.exec(sqlDel);if (!ret){outPutMsg(QtDebugMsg, "DataModel::updateProInfosByProNo error = " + query.lastError().text());return false;}try {for (int i = 0; i < proInfoList.size(); i++) {QStringList programInfo = proInfoList.at(i).split(";");QString sqlOne = "insert into programInfo values ( " + proNo + ",";for (int j = 0; j < programInfo.size(); j++) {if (j > 0) {sqlOne += ",";}sqlOne += programInfo.at(j) ;}sqlOne += ");";outPutMsg(QtDebugMsg, "DataModel::updateProInfosByProNo sqlOne = " + sqlOne);if (!query.exec(sqlOne)){outPutMsg(QtDebugMsg, "DataModel::updateProInfosByProNo error = " + query.lastError().text());return false;}}}catch (...) {outPutMsg(QtDebugMsg, "DataModel::updateProInfosByProNo error = " + query.lastError().text());return false;}// 开启事务if (!db.commit()) {outPutMsg(QtDebugMsg, "DataModel::updateProInfosByProNo db.error = " + db.lastError().text());return false;}return true;
}
// 保存程序信息
void ClearCorner::on_edit_saveBtn_clicked() {QStringList proInfoList;for (int i = 0; i < modelProgram.rowCount(); i++) {QString strTmp;for (int j = 0; j < modelProgram.columnCount(); j++) {QStandardItem* item = modelProgram.item(i, j);if (j > 0) {strTmp += ";";}strTmp += item->text();}outPutMsg(QtDebugMsg, "ClearCorner::on_edit_saveBtn_clicked strTmp = " + strTmp);proInfoList.append(strTmp);}DataModel dataModel;bool ret = dataModel.updateProInfosByProNo(ui.edit_lineEdit_programNo->text(), proInfoList);if (ret) {QMessageBox::information(nullptr, "提示", "更新程序成功!");}else {QMessageBox::warning(nullptr, "提示", "更新程序失败!");}
}
http://www.lryc.cn/news/397660.html

相关文章:

  • 【深度学习入门篇 ②】Pytorch完成线性回归!
  • Syslog 管理工具
  • 硅纪元AI应用推荐 | 百度橙篇成新宠,能写万字长文
  • Codeforces Round 954 (Div. 3)
  • 【Django】报错‘staticfiles‘ is not a registered tag library
  • LeetCode 算法:二叉树的最近公共祖先 III c++
  • Windows CMD 命令汇总表
  • 【python+appium】自动化测试
  • vue 数据类型
  • MySQL(基础篇)
  • springboot中通过jwt令牌校验以及前端token请求头进行登录拦截实战
  • 从零开始开发视频美颜SDK:实现直播美颜效果
  • 极验语序点选验证码识别(一)
  • 什么是 HTTP POST 请求?初学者指南与示范
  • 第一次作业
  • 【机器学习】12.十大算法之一支持向量机(SVM - Support Vector Machine)算法原理讲解
  • 使用 `useAppConfig` :轻松管理应用配置
  • 中国内陆水体氮沉降数据集(1990s-2010s)
  • qml 实现一个带动画的switch 按钮
  • C语言基本概念
  • 同轴多芯旋转电连接器1
  • android 消除内部保存的数据
  • vue3 ts 报错:无法找到模块“../views/index/Home.vue”的声明文件
  • finalshell发布前端项目到阿里云
  • 纹波电流与ESR:解析电容器重要参数与应用挑战
  • 算法——二分法
  • 「PaddleOCR」 模型应用优化流程
  • VUE2 子组件传多个参数,父组件函数接收所有入参并加自定义参数
  • less和sass有啥区别哪个更加好
  • Qt Design Studio 4.5现已发布