Qt database application 17 - general database request

I. Introduction

The database request is based on the database collection and is replaced by the form of http request to obtain data, which is separated from the database component, and the general http request is used to obtain the database from the database. This versatility is very extensive, such as The other party uses java to collect the data of the device and stores it in the database, and the other area uses the data collected by c# to store it, and then unifies the rules and specifications to agree on a request mechanism, the timer or thread fetches the execution request, and the obtained json data, one by one The result is taken out, and the signal is sent out in the form of an array collection.

The visual large-screen electronic kanban program I wrote before also uses the method of HTTP request database collection to display data, which requires multi-party cooperation. As for who puts the data in the database, it has nothing to do with the large-screen program itself, and the corresponding can be a request. The data stored in the form of . Hands, hearts have coordinates, everything is a painter.

Some experience summary about Qt database related development:
https://qtchina.blog.csdn.net/article/details/119022424

2. Features

  1. At the same time, it supports a variety of databases such as odbc, sqlite, mysql, postgresql, sqlserver, oracle, NPC Jincang, etc.
  2. A single database class can manage local database communication, as well as support remote database communication, etc.
  3. The database thread supports the execution of various SQL statements, including single and batch.
  4. All class printing information, error information, and execution results in the component are signaled.
  5. Integrate the general page-turning class of the database (responsible for specific processing logic), and match the paging navigation control (responsible for appearance) to form a super awesome page-turning control.
  6. Integrated database automatic cleaning class, setting the maximum number of records to automatically clean up early data in the background.
  7. Integrate custom delegate classes to support checkboxes, text boxes, drop-down boxes, date boxes, spinners, progress bars, etc.
  8. At the same time support Qt4-Qt6, test any version of Qt4.6 to Qt6.3, any system and compiler.
  9. This component runs at least tens of thousands of sites 7 times 24 hours a day, 360 days without failure, with commercial-level quality assurance.
  10. Each class corresponds to a complete and detailed usage example, with detailed annotations, which is very suitable for reading and learning.
  11. It can be run as a standalone program, such as automatically cleaning up old data and syncing data to the cloud.
  12. All threads are processed, no interface is stuck, and the database is automatically reconnected.
  13. In the normal test situation, the sqlite database, the database generator inserts 1000 records per second for about 0.003 seconds, while the automatic cleaning data class deletes 1000 records per second for about 0.13 seconds, and different threads do not interfere with each other.

3. Experience address

  1. Experience address: https://pan.baidu.com/s/1ZxG-oyUKe286LPMPxOrO2A Extraction code: o05q File name: bin_dbtool.zip
  2. Domestic site: https://gitee.com/feiyangqingyun
  3. International site: https://github.com/feiyangqingyun
  4. Personal homepage: https://blog.csdn.net/feiyangqingyun
  5. Know the homepage: https://www.zhihu.com/people/feiyangqingyun/

Fourth, the effect map

insert image description here

5. Relevant code

#include "frmdbhttp.h"
#include "ui_frmdbhttp.h"
#include "quihelper.h"
#include "dbhelper.h"
#include "dbhttpthread.h"

frmDbHttp::frmDbHttp(QWidget *parent) : QWidget(parent), ui(new Ui::frmDbHttp)
{
    
    
    ui->setupUi(this);
    this->initForm();
    this->initConfig();
    this->initTable();
}

frmDbHttp::~frmDbHttp()
{
    
    
    delete ui;
}

void frmDbHttp::showEvent(QShowEvent *)
{
    
    
    static bool isShow = false;
    if (!isShow) {
    
    
        isShow = true;
        QTimer::singleShot(100, this, SLOT(on_btnStart_clicked()));
    }
}

void frmDbHttp::initForm()
{
    
    
    ui->frame->setFixedWidth(AppConfig::RightWidth);
    QUIHelper::initTableView(ui->tableWidget);

    maxCount = 100;
    currentCount = 0;

    timer = new QTimer(this);
    timer->setInterval(1000);
    connect(timer, SIGNAL(timeout()), this, SLOT(on_btnDo_clicked()));

    //实例化网络请求采集数据通信类
    dbHttp = new DbHttpThread(this);
    connect(dbHttp, SIGNAL(debug(QString)), this, SLOT(debug(QString)));
    connect(dbHttp, SIGNAL(error(QString)), this, SLOT(error(QString)));
    connect(dbHttp, SIGNAL(receiveData(QString, QStringList, int)), this, SLOT(receiveData(QString, QStringList, int)));

    //设置参数
    dbHttp->setUrl("http://127.0.0.1:6000");
    QMap<QString, QString> tables;
    tables.insert("NodeData", "PositionID,NodeValue,NodeStatus,SaveTime");
    dbHttp->setTables(tables);
}

void frmDbHttp::initConfig()
{
    
    

}

void frmDbHttp::saveConfig()
{
    
    

}

void frmDbHttp::initTable()
{
    
    
    QStringList columnNames;
    columnNames << "位号" << "当前值" << "状态" << "时间";
    QList<int> columnWidths;
    columnWidths << 160 << 100 << 100 << 220;

    columnCount = columnNames.count();
    ui->tableWidget->setColumnCount(columnCount);
    ui->tableWidget->setHorizontalHeaderLabels(columnNames);
    for (int i = 0; i < columnCount; i++) {
    
    
        ui->tableWidget->setColumnWidth(i, columnWidths.at(i));
    }
}

void frmDbHttp::debug(const QString &msg)
{
    
    
    QUIHelper::appendMsg(ui->txtMain, 0, msg, maxCount, currentCount);
}

void frmDbHttp::error(const QString &msg)
{
    
    
    QUIHelper::appendMsg(ui->txtMain, 1, msg, maxCount, currentCount);
}

void frmDbHttp::receiveData(const QString &tag, const QStringList &data, int msec)
{
    
    
    QString msg = QString("用时( %1 秒)  标识( %2 )  数据( %3 )").arg(QString::number((double)msec / 1000, 'f', 3)).arg(tag).arg(data.join("|"));
    QUIHelper::appendMsg(ui->txtMain, 3, msg, maxCount, currentCount);

    //显示到表格中
    int count = data.count();
    ui->tableWidget->setRowCount(count / columnCount);
    int row = -1;
    for (int i = 0; i < count; i = i + columnCount) {
    
    
        row++;
        for (int j = 0; j < columnCount; ++j) {
    
    
            ui->tableWidget->setItem(row, j, new QTableWidgetItem(data.at(i + j)));
        }
    }
}

void frmDbHttp::on_btnDo_clicked()
{
    
    
    dbHttp->select("NodeData", "PositionID,NodeValue,NodeStatus,SaveTime");
}

void frmDbHttp::on_btnStart_clicked()
{
    
    
    if (ui->btnStart->text() == "启动服务") {
    
    
        on_btnDo_clicked();
        timer->start();
        ui->btnStart->setText("停止服务");
    } else {
    
    
        timer->stop();
        ui->btnStart->setText("启动服务");
    }
}

void frmDbHttp::on_btnClear_clicked()
{
    
    
    QUIHelper::appendMsg(ui->txtMain, 0, "", maxCount, currentCount);
}

Guess you like

Origin blog.csdn.net/feiyangqingyun/article/details/123651490