首页 > 数据库 >QT--SQLite

QT--SQLite

时间:2024-07-10 09:29:55浏览次数:20  
标签:SQLite QT outPutMsg -- db QString sql query DataModel

配置类相关的表,所以我使用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() + ";"); // sectionBarName
        program += 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;
    // 根据名字查询
    // 使用索引sectionBarName
    QString 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())+";"); // stepNo
        programInfo += (QString::number(query.value(1).toInt())+";"); // cutNo
        programInfo += (QString::number(query.value(2).toDouble(), 'f', 2) + ";"); // x
        programInfo += (QString::number(query.value(3).toDouble(), 'f', 2) + ";"); // y
        programInfo += (QString::number(query.value(4).toDouble(), 'f', 2) + ";"); // f 速度
        programInfo += (QString::number(query.value(5).toDouble(), 'f', 2) + ";"); // Rx
        programInfo += (QString::number(query.value(6).toDouble(), 'f', 2)); // Ry
        
        outPutMsg(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;
    // 根据名字查询
    // 使用索引sectionBarName
    QString 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, "提示", "更新程序失败!");
    }
}

标签:SQLite,QT,outPutMsg,--,db,QString,sql,query,DataModel
From: https://blog.csdn.net/qq_23136415/article/details/140313915

相关文章

  • 【转】-Java CAS 原理剖析
    JavaCAS原理剖析本文转载来自​卡巴拉的树​的​JavaCAS原理剖析在Java并发中,我们最初接触的应该就是synchronized关键字了,但是synchronized属于重量级锁,很多时候会引起性能问题,volatile也是个不错的选择,但是volatile不能保证原子性,只能在某些场合下使用。像synchronized这......
  • Impala写Parquet文件
    ImpalaParquet相关代码  https://github.com/cloudera/Impala/search?l=cpp&q=parquet&ref=cmdform   没有可重用的库接口,需要在代码里去看,提取出来,直接使用源码。 调用关系如下(自右向左调用): HdfsParquetTableWriter(HdfsTableWriter)<-HdfsTableSink<-DataSin......
  • 搭建自己的局域网,在自己电脑上搭建DHCP服务器
    文章目录前言一、dhcp是什么?二、软件下载2.开启服务总结前言我用我的笔记本直接用一根网线连接B的电脑,这样可以进行共享吗?答案是否。那怎么才能通过一根网线就实现上述功能呢?答案就是在自己电脑上搭建一个dhcp服务器。搭建dhcp服务器的作用:自己电脑搭建完dhcp服......
  • Parquet && Impala
    参考官网:Parquet: ParquetImpala: ImpalaParquet:https://github.com/Parquet/parquet-format MetadataTherearethreetypesofmetadata:filemetadata,column(chunk)metadataandpageheadermetadata.AllthriftstructuresareserializedusingtheTCompa......
  • 解决PyTorch中的RuntimeError: CUDA error: device-side assert triggered
    解决PyTorch中的RuntimeError:CUDAerror:device-sideasserttriggered......
  • 开启新纪元!被AI驱动的游戏世界,提升游戏体验
     随着人工智能的高速发展,人工智能逐渐应用到了生活中的方方面面,人工智能在游戏中也有诸多应用,在游戏里领域扮演了相当重要的角色。游戏AI是伴随着电子游戏而出现的,在早期的游戏中就出现了对抗类AI角色,后来逐渐出现了更复杂的NPCAI,除通常理解的游戏AI之外,语音、视觉、机器学习......
  • 如何修复TensorFlow中的OutOfRangeError:迭代器数据耗尽
    如何修复TensorFlow中的OutOfRangeError:迭代器数据耗尽......
  • 量化交易策略:赌徒在股市会运用凯利公式(附python代码)
    一、凯利公式的历史凯利公式(KellyCriterion)是由美国贝尔实验室物理学家约翰·拉里·凯利(JohnLarryKelly)于1956年提出的,用于计算最优投资比例的一种数学公式。凯利公式的核心思想是:在期望收益和风险之间找到一个平衡点,使得投资者在承担一定风险的情况下,能够获得最大化的......
  • 计算机毕业设计项目:18655 课程题库管理系统(开题答辩+程序定制+全套文案 )上万套实战教
    摘 要随着科学技术的飞速发展,各行各业都在努力与现代先进技术接轨,通过科技手段提高自身的优势;对于课程题库管理系统当然也不能排除在外,随着网络技术的不断成熟,带动了课程题库管理系统,它彻底改变了过去传统的管理方式,不仅使服务管理难度变低了,还提升了管理的灵活性。这种个......
  • 计算机毕业设计项目: node.js 网上购物商城的设计与实现99525(开题答辩+程序定制+全套文
    摘 要随着社会的发展,计算机的优势和普及使得网上购物商城的开发成为必需。网上购物商城主要是借助计算机,通过对首页、站点管理(轮播图、公告栏)用户管理(管理员、注册用户)内容管理(商城资讯、资讯分类)商城管理(商城中心、分类列表、订单列表)等信息进行管理。减少管理员的工作......