• QT 之数据库 QSqlQuery CURD 实战


    零、参考文档

    https://doc.qt.io/archives/qt-6.0/qsqldatabase.html

    一、开发环境

    1. Ubuntu 20.04
    2. QT6.0
    3. Microsoft SQL Server 2022 Developer Edition (64-bit)

    先修改 /etc/odbc.ini 的数据源配置,指定连接数据库 vdb,

    1. sudo vim /etc/odbc.ini
    2. [mssql]
    3. Driver=MSSQL
    4. #USER=sa
    5. #Password=123456789
    6. PORT=1433
    7. SERVER=localhost
    8. Database=vdb

    创建项目,

    pro 配置开启 sql 模块,

    QT += sql

    新建头文件 connection.h,

    1. // connection.h
    2. #ifndef CONNECTION_H
    3. #define CONNECTION_H
    4. #include
    5. #include
    6. #include
    7. static bool connect_mssql(){
    8. // 数据库配置
    9. QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
    10. QString dsn = QString::fromLocal8Bit("mssql");
    11. // 数据源名称
    12. db.setDatabaseName(dsn);
    13. // 服务器名称
    14. db.setHostName("localhost");
    15. db.setPort(1433);
    16. // 用户名
    17. db.setUserName("sa");
    18. // 密码
    19. db.setPassword("123456789");
    20. // 数据库连接
    21. bool ok = db.open();
    22. if(ok)
    23. {
    24. qDebug() << "db open ok!";
    25. }
    26. else {
    27. qDebug() << "db open error: " << db.lastError();
    28. }
    29. return ok;
    30. }
    31. #endif // CONNECTION_H

    然后再在 main.cpp 中引用这个头文件,

    1. // main.cpp
    2. #include "mainwindow.h"
    3. #include "connection.h"
    4. #include
    5. int main(int argc, char *argv[])
    6. {
    7. if(!connect_mssql()){
    8. return 1;
    9. }
    10. QApplication a(argc, argv);
    11. MainWindow w;
    12. w.show();
    13. return a.exec();
    14. }

    二、QSqlQuery

    主窗口添加四个 Push Button,为四个按钮绑定单击信号槽函数,

    然后在 mainwindow.cpp 分别实现四个槽函数,

    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include
    4. #include
    5. #include
    6. #include
    7. MainWindow::MainWindow(QWidget *parent)
    8. : QMainWindow(parent)
    9. , ui(new Ui::MainWindow)
    10. {
    11. ui->setupUi(this);
    12. }
    13. MainWindow::~MainWindow()
    14. {
    15. delete ui;
    16. }
    17. void MainWindow::on_query_btn_clicked()
    18. {
    19. QSqlQuery query;
    20. // 查找表中 color = LightPink 的数据
    21. query.exec("select id, color, rgb, del_flag from vdb.dbo.color_define where color = 'LightPink'");
    22. while(query.next())
    23. {
    24. int id = query.value(0).toInt();
    25. QString color = query.value(1).toString();
    26. QString rgb = query.value(2).toString();
    27. int del_flag = query.value(3).toInt();
    28. // 输出
    29. qDebug() << id << " " << color << " " << rgb << " " << del_flag;
    30. }
    31. }
    32. void MainWindow::on_insert_btn_clicked()
    33. {
    34. // 创建 QSqlQuery 对象
    35. QSqlQuery query;
    36. if (!query.exec("insert into vdb.dbo.color_define(color,rgb,del_flag) values('LightPink','255,182,193',0)"))
    37. {
    38. qDebug() << query.lastError();
    39. }
    40. }
    41. void MainWindow::on_update_btn_clicked()
    42. {
    43. // 创建 QSqlQuery 对象
    44. QSqlQuery query;
    45. if (!query.exec("update vdb.dbo.color_define set del_flag = 1 where color = 'LightPink'"))
    46. {
    47. qDebug() << query.lastError();
    48. }
    49. }
    50. void MainWindow::on_delete_btn_clicked()
    51. {
    52. // 创建 QSqlQuery 对象
    53. QSqlQuery query;
    54. if (!query.exec("delete vdb.dbo.color_define where color = 'LightPink'"))
    55. {
    56. qDebug() << query.lastError();
    57. }
    58. }

    三、参数绑定

    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include
    4. #include
    5. #include
    6. #include
    7. MainWindow::MainWindow(QWidget *parent)
    8. : QMainWindow(parent)
    9. , ui(new Ui::MainWindow)
    10. {
    11. ui->setupUi(this);
    12. }
    13. MainWindow::~MainWindow()
    14. {
    15. delete ui;
    16. }
    17. void MainWindow::on_query_btn_clicked()
    18. {
    19. QSqlQuery query;
    20. // 查找表中 color = LightPink 的数据
    21. query.prepare("select id, color, rgb, del_flag from vdb.dbo.color_define where color = :color");
    22. query.bindValue(":color","LightPink");
    23. query.exec();
    24. while(query.next())
    25. {
    26. int id = query.value(0).toInt();
    27. QString color = query.value(1).toString();
    28. QString rgb = query.value(2).toString();
    29. int del_flag = query.value(3).toInt();
    30. // 输出
    31. qDebug() << id << " " << color << " " << rgb << " " << del_flag;
    32. }
    33. }
    34. void MainWindow::on_insert_btn_clicked()
    35. {
    36. // 创建 QSqlQuery 对象
    37. QSqlQuery query;
    38. query.prepare("insert into vdb.dbo.color_define(color,rgb,del_flag) values(:color,:rgb,:del_flag)");
    39. query.bindValue(0,"LightPink");
    40. query.bindValue(1,"255,182,193");
    41. query.bindValue(2,0);
    42. if (!query.exec())
    43. {
    44. qDebug() << query.lastError();
    45. }
    46. }
    47. void MainWindow::on_update_btn_clicked()
    48. {
    49. // 创建 QSqlQuery 对象
    50. QSqlQuery query;
    51. query.prepare("update vdb.dbo.color_define set del_flag = 1 where color = :color");
    52. query.addBindValue("LightPink");
    53. if (!query.exec())
    54. {
    55. qDebug() << query.lastError();
    56. }
    57. }
    58. void MainWindow::on_delete_btn_clicked()
    59. {
    60. // 创建 QSqlQuery 对象
    61. QSqlQuery query;
    62. query.prepare("delete vdb.dbo.color_define where color = ?");
    63. //query.addBindValue("LightPink");
    64. query.bindValue(0,"LightPink");
    65. if (!query.exec())
    66. {
    67. qDebug() << query.lastError();
    68. }
    69. }

    四、批量处理

    1. void MainWindow::on_batch_btn_clicked()
    2. {
    3. // 创建 QSqlQuery 对象
    4. QSqlQuery query;
    5. query.prepare("insert into vdb.dbo.color_define(color,rgb,del_flag) values(?, ?, ?)");
    6. QVariantList colors;
    7. colors << "LightPink" << "OliveDrab" << "Tomato";
    8. query.addBindValue(colors);
    9. QVariantList rgbs;
    10. rgbs << "255,182,193" << "85,107,47" << "255,99,71";
    11. query.addBindValue(rgbs);
    12. QVariantList flags;
    13. flags << 0 << 0 << 0 ;
    14. query.addBindValue(flags);
    15. if (!query.execBatch())
    16. {
    17. qDebug() << query.lastError();
    18. }
    19. if(!query.exec("select top(100) * from vdb.dbo.color_define(nolock)")){
    20. qDebug() << query.lastError();
    21. }
    22. while(query.next())
    23. {
    24. int id = query.value(0).toInt();
    25. QString color = query.value(1).toString();
    26. QString rgb = query.value(2).toString();
    27. int del_flag = query.value(3).toInt();
    28. // 输出
    29. qDebug() << id << " " << color << " " << rgb << " " << del_flag;
    30. }
    31. }

    五、事务

    1. void MainWindow::on_delete_btn_clicked()
    2. {
    3. // transaction start
    4. QSqlDatabase::database().transaction();
    5. // 创建 QSqlQuery 对象
    6. QSqlQuery query;
    7. query.prepare("delete vdb.dbo.color_define where color = ?");
    8. query.addBindValue("LightPink");
    9. //query.bindValue(0,"LightPink");
    10. if (!query.exec())
    11. {
    12. qDebug() << query.lastError();
    13. }
    14. // transaction commit
    15. QSqlDatabase::database().commit();
    16. }
  • 相关阅读:
    Lambda 表达式原理分析学习(2022.06.23)
    分享一个超好看回忆相册(代码自取)
    DBAPI如何使用数组类型参数
    什么是枚举类型?如何定义和使用枚举?
    Nginx(五) break,if,return,rewrite和set指令的执行顺序深究
    基于贝塞尔曲线(Bézier Curve)与硬约束的轨迹优化方法
    深入理解强化学习——多臂赌博机:梯度赌博机算法的数学证明
    基于springboot+vue的疫情期间外出务工人员信息管理系统
    基于STM32F103ZET6库函数按键输入实验
    小白网络安全学习手册
  • 原文地址:https://blog.csdn.net/weixin_47560078/article/details/132857865