数据库采集对应的就是上一篇文章的数据库同步,数据库同步到云端数据库以后,app、网页、小程序啥的要数据的话,可以通过执行http请求拿到数据,http接收应答这边程序一般最简单可以用php写,几行代码即可,比如根据请求的参数构建需要执行的sql语句,执行以后将结果json形式返回即可。
通过Qt程序去做数据库采集有多种方式,即可直接连接数据库然后绑定数据库表到QSqlTableModel,然后不断调用select方法查询刷新界面即可,也可通过执行sql语句的形式,通过sql语句的形式更灵活,可以先拿到数据结果集合,然后过滤,比如判断是否报警了,是否需要执行其他指令动作,然后再将需要的数据信号的形式发出去,需要的地方关联信号拿到数据展示即可,相当于做了一层筛选。
数据库通信管理线程类特点:
关于Qt数据库相关开发的一些经验总结:
https://qtchina.blog.csdn.net/article/details/119022424
#include "frmdbdownload.h" #include "ui_frmdbdownload.h" #include "quihelper.h" #include "dbconnthread.h" frmDbDownload::frmDbDownload(QWidget *parent) : QWidget(parent), ui(new Ui::frmDbDownload) { ui->setupUi(this); this->initForm(); this->initConfig(); this->initTable(); } frmDbDownload::~frmDbDownload() { delete ui; } void frmDbDownload::initForm() { ui->frame->setFixedWidth(AppConfig::RightWidth); QUIHelper::initTableView(ui->tableWidget); maxCount = 100; currentCount = 0; columnCount = 0; timer = new QTimer(this); timer->setInterval(1000); connect(timer, SIGNAL(timeout()), this, SLOT(on_btnDo_clicked())); //实例化数据库通信类 dbConn = new DbConnThread(this); dbConn->setDbFlag("云端"); connect(dbConn, SIGNAL(debug(QString)), this, SLOT(debug(QString))); connect(dbConn, SIGNAL(error(QString)), this, SLOT(error(QString))); connect(dbConn, SIGNAL(receiveData(QString, QStringList, int)), this, SLOT(receiveData(QString, QStringList, int))); } void frmDbDownload::initConfig() { ui->cboxDbType->addItems(DbHelper::getDbType()); ui->cboxDbType->setCurrentIndex(ui->cboxDbType->findText(AppConfig::DbType6)); connect(ui->cboxDbType, SIGNAL(currentIndexChanged(int)), this, SLOT(saveConfig())); ui->txtDbName->setText(AppConfig::DbName6); connect(ui->txtDbName, SIGNAL(textChanged(QString)), this, SLOT(saveConfig())); ui->txtHostName->setText(AppConfig::HostName6); connect(ui->txtHostName, SIGNAL(textChanged(QString)), this, SLOT(saveConfig())); ui->txtHostPort->setText(QString::number(AppConfig::HostPort6)); connect(ui->txtHostPort, SIGNAL(textChanged(QString)), this, SLOT(saveConfig())); ui->txtUserName->setText(AppConfig::UserName6); connect(ui->txtUserName, SIGNAL(textChanged(QString)), this, SLOT(saveConfig())); ui->txtUserPwd->setText(AppConfig::UserPwd6); connect(ui->txtUserPwd, SIGNAL(textChanged(QString)), this, SLOT(saveConfig())); } void frmDbDownload::saveConfig() { AppConfig::DbType6 = ui->cboxDbType->currentText(); AppConfig::DbName6 = ui->txtDbName->text(); AppConfig::HostName6 = ui->txtHostName->text(); AppConfig::HostPort6 = ui->txtHostPort->text().toInt(); AppConfig::UserName6 = ui->txtUserName->text(); AppConfig::UserPwd6 = ui->txtUserPwd->text(); AppConfig::writeConfig(); } void frmDbDownload::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 frmDbDownload::debug(const QString &msg) { QUIHelper::appendMsg(ui->txtMain, 0, msg, maxCount, currentCount); } void frmDbDownload::error(const QString &msg) { QUIHelper::appendMsg(ui->txtMain, 1, msg, maxCount, currentCount); } void frmDbDownload::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 frmDbDownload::on_btnOpen_clicked() { if (ui->btnOpen->text() == "打开数据库") { DbInfo dbInfo; dbInfo.connName = this->objectName(); dbInfo.dbName = AppConfig::DbName6; dbInfo.hostName = AppConfig::HostName6; dbInfo.hostPort = AppConfig::HostPort6; dbInfo.userName = AppConfig::UserName6; dbInfo.userPwd = AppConfig::UserPwd6; QString dbType = AppConfig::DbType6.toUpper(); if (dbType == "SQLITE") { dbInfo.dbName = DbHelper::getDbDefaultFile(); } dbConn->setConnInfo(DbHelper::getDbType(dbType), dbInfo); if (dbConn->openDb()) { dbConn->start(); ui->btnOpen->setText("关闭数据库"); } else { QString error = dbConn->getDatabase().lastError().text(); QUIHelper::showMessageBoxError("打开数据库失败!\n" + error, 3); } } else { dbConn->stop(); dbConn->closeDb(); ui->btnOpen->setText("打开数据库"); on_btnClear_clicked(); } QTimer::singleShot(1000, this, SLOT(on_btnStart_clicked())); } void frmDbDownload::on_btnCopy_clicked() { //将数据库设置参数一键粘贴过来 ui->cboxDbType->setCurrentIndex(ui->cboxDbType->findText(AppConfig::LocalDbType)); ui->txtDbName->setText(AppConfig::LocalDbName); ui->txtHostName->setText(AppConfig::LocalHostName); ui->txtHostPort->setText(QString::number(AppConfig::LocalHostPort)); ui->txtUserName->setText(AppConfig::LocalUserName); ui->txtUserPwd->setText(AppConfig::LocalUserPwd); } void frmDbDownload::on_btnDo_clicked() { if (!dbConn->getOk()) { return; } dbConn->select("NodeData", "PositionID,NodeValue,NodeStatus,SaveTime"); } void frmDbDownload::on_btnStart_clicked() { if (ui->btnStart->text() == "启动服务") { on_btnDo_clicked(); timer->start(); ui->btnStart->setText("停止服务"); } else { timer->stop(); ui->btnStart->setText("启动服务"); } } void frmDbDownload::on_btnClear_clicked() { QUIHelper::appendMsg(ui->txtMain, 0, "", maxCount, currentCount); }